Fixing error: SyntaxError: Identifier ‘jest’ has already been declared in file ‘dist\__tests__\import-editions-test.js’.

The following error can occur using Jest:

Using Jest CLI v13.2.3, jasmine2
 FAIL  dist\__tests__\import-editions-test.js (0s)
● Runtime Error
  - SyntaxError: Identifier 'jest' has already been declared in file 'dist\__tests__\import-editions-test.js'.

    Make sure your preprocessor is set up correctly and ensure your 'preprocessorIgnorePatterns' configuration is correct: http://facebook.github.io/jest/docs/api.html#preprocessorignorepatterns-array-string
    If you are currently setting up Jest or modifying your preprocessor, try `jest --no-cache`.
    Preprocessor: No preprocessor specified, consider installing 'babel-jest'.
    Jest tried to the execute the following code:
    "use strict";
    const import_editions_1 = require('../import-editions');
    const jest = require('jest');
    jest.unmock('../import-editions');
    describe('parseLine', () => {
        it('parses the header part of the line', () => {
            expect(import_editions_1.parseLine('abc')).toBe('abc');
        });
    });


        at Runtime._runSourceText (node_modules\jest-runtime\build\index.js:410:15)
        at Runtime._execModule (node_modules\jest-runtime\build\index.js:374:30)
        at Runtime.requireModule (node_modules\jest-runtime\build\index.js:210:14)
        at jasmine2 (node_modules\jest-jasmine2\build\index.js:293:11)
        at Test.run (node_modules\jest-cli\build\Test.js:50:12)
        at promise.then.then.data (node_modules\jest-cli\build\TestRunner.js:264:62)
1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 1.086s)
npm ERR! Test failed.  See above for more details.

This indicates that you tried to import Jest into your file – it is implicitly added for you as a global when you run tests. So, don’t do this:

const jest = require('jest');

One Reply to “Fixing error: SyntaxError: Identifier ‘jest’ has already been declared in file ‘dist\__tests__\import-editions-test.js’.”

Leave a Reply

Your email address will not be published. Required fields are marked *