I am currently setting up unit tests with usage of following stack:
- react (v15) components are written with typescript (.tsx)
- test setup is done with jest(v21) and enzyme(v3)
- test files are written as plain js-files
Unfortunately something seems to go wrong with enzyme as I keep getting an error:
wrapper = enzyme.shallow(<Stepper>bla</Stepper>)
^
SyntaxError: Unexpected token <
at new Script (vm.js:51:7)
at Generator.next (<anonymous>)
at new Promise (<anonymous>)
Test Suites: 1 failed, 1 total
The respective test file looks like this:
var React = require('react');
var enzyme = require('enzyme');
var Stepper = require('./Stepper').default;
var wrapper;
describe('Stepper', () => {
beforeEach(() => {
wrapper = enzyme.shallow(<Stepper>bla</Stepper>)
});
test('has bla', () => {
expect(wrapper.contains('bla')).toBeTruthy();
});
});
I configured jest as followed in my package.json:
"jest": {
"transform": {
"^.+\\.(tsx|ts)$": "typescript-babel-jest"
},
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"testMatch": [
"<rootDir>/src/**/**/*.test.js"
],
"moduleNameMapper": {
"\\.(css|scss)$": "identity-obj-proxy"
},
"setupTestFrameworkScriptFile": "<rootDir>/setupTests.js"
}
And my setupTests.js file looks like this:
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-15');
enzyme.configure({ adapter: new Adapter() });
I am running out of ideas what could be causing the issue, does anyone know a solution to this?
<Stepper>bla</Stepper>
is not correctly interpreted as if React was not correctly imported into this scope. Is there a certain reason you are using requirejs instead of es6 modules? – Repetition<div></div>
is also jsx and you can only write jsx if React was correctly imported. I guess the reason is, that babel needs to run on your test file which has a.js
ending, but you are only transforming.tsx
and.ts
. Try to add.js
to your jest transforms in yourpackage.json
. I can only strongly recommend you to make yourself familiar with webpack + babel and es6 as it makes your life much easier writing code. – Repetition