Hi all,
I’m working on the Reddit project here and I’m facing some issues when it comes to testing. I 'm trying to test one of my actions:
import {setSearchTerm} from '../app/redditSlice.js';
//const setSearchTerm = require('../app/redditSlice');
describe('Search term function', ()=>{
test('It should set the search term (action.payload)', ()=>{
let state = {
isLoading: false,
selectedSubreddit: 'r/funny',
searchTerm: '',
error: false,
posts: [],
subreddits: [],
}
const input = {type:'reddit/setSearchTerm', payload:'world'};
const output = {
isLoading: false,
selectedSubreddit: 'r/funny',
searchTerm: 'world',
error: false,
posts: [],
subreddits: [],
}
setSearchTerm(state, input);
expect(state).toEqual(output);
});
});
The first issue is when I try to run the test using jest, setting "test": "jest",
in my package.json. When I run npm test
I get the following error:
[email protected]:~/Documents/reddit-clone$ npm test
> [email protected] test
> jest
FAIL src/__tests__/tests.spec.js
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/home/dev/Documents/reddit-clone/src/app/redditSlice.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { createSlice, createSelector } from '@reduxjs/toolkit';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | const setSearchTerm = require('../app/redditSlice');
| ^
2 |
3 | describe('Search term function', ()=>{
4 | test('It should set the search term (action.payload)', ()=>{
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/__tests__/tests.spec.js:1:52)
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:404:19)
at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.236 s
Ran all test suites.
If I set "test" :"react-script test"
, the tests seem to be running but they fail with the following output:
FAIL src/__tests__/tests.spec.js
Search term function
✕ It should set the search term (action.payload) (4 ms)
● Search term function › It should set the search term (action.payload)
expect(received).toEqual(expected) // deep equality
- Expected - 1
+ Received + 1
Object {
"error": false,
"isLoading": false,
"posts": Array [],
- "searchTerm": "world",
+ "searchTerm": "",
"selectedSubreddit": "r/funny",
"subreddits": Array [],
}
24 | setSearchTerm(state, input);
25 |
> 26 | expect(state).toEqual(output);
| ^
27 | });
28 | });
29 |
at Object.<anonymous> (src/__tests__/tests.spec.js:26:23)
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:404:19)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 0.586 s
which means the call to setSearchTerm(state, input);
is not working.
You can see the whole project here.
Help for either of my issues would be much appreciated thanks !