Jest tests broken after implementing detox - react-native

I'd like to be able to run my detox tests and my Jest unit tests separately. For example, run detox tests with detox build && detox test, and my Jest unit tests with npm test.
After implementing detox (using mocha as the test runner), running npm test results in immediate error, and looks like its trying to run my detox tests (not what I'd expect)! Here's the first error I get.
FAIL e2e/auth.spec.js
Not sure why its trying to run detox tests, when my package.json is pointing the test script to Jest.
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
}
How do I run my jest tests now?

By default jest runs all files in your project directory, that have the .test. or .spec. extension to them. That's why it picks up your detox test files and fails to execute them.
https://facebook.github.io/jest/docs/en/configuration.html#testmatch-array-string
You have to override this default behavior in order for the two not to clash. Here's what we use in our package.json just for reference, you might want to change it:
"jest": {
"testMatch": [
"<rootDir>/__tests__/**/*.test.js?(x)",
"<rootDir>/src/**/*.test.js"
]
}

If you dont keep you files in on folder like tests just add this to package.json
"jest": {
"testMatch": ["**/*+(.test.js)"]
}

Related

Custom timezone for tests when running from intellij IDEA

I am trying to migrate tests from jest to vitest. For all of my tests I need to use UTC timezone. Currently, I was able to achieve it by TZ=UTC in package.json (refer: https://github.com/vitest-dev/vitest/issues/1575#issuecomment-1171085461).
"scripts": {
"dev": "vite",
"serve": "vite preview",
"build": "vite build",
"test": "TZ=UTC vitest run",
"test:watch": "TZ=UTC vitest"
}
My all the tests pass successfully when I am running the tests from CLI by using npm test. But when it comes to run tests from intellij looks like the tests aren't picking the timezone information from package.json. Is it expected behaviour?
I am using v0.6.6 of vitest runner and IntelliJ IDEA 2022.2.3.
You should specify this option in the JetBrains screen instead of package.json, as they are not using CLI as you.
https://blog.jetbrains.com/webstorm/2022/11/webstorm-2022-3-beta/

How to run shell scripts cross-platform, using npm

I have nestJS project with that line in package.json:
"scripts": {
...
"test": "sh scripts/test.sh",
...
},
And on my windows machine, it can't run.
How to rewrite it, to run cross-platform? May be example using cross-env-shell or add some package in dependencies?

How to make a dynamic path on a package.json script

On my package.json I have this script
"test": "hardhat test".
I would like to make another one to only run tests from a subfolder, and not the entire test suite.
Something like "test:single": "hardhat test ./tests/subfolder/${runThisOne}" and call it like yarn test:single coolTest which would result in yarn test:single ./tests/subfolder/coolTest. Is this possible?
For Npm use this,
"scripts":{
"test": "hardhat test",
"sale": "hardhat test test/Sale.js" // here test will run sale.js
}

npm scripts not running in series

I want to run eslint as a prestart script so if it fails it won't run the build script, but even when the eslint returns "clean" result it still stops the following scripts from running.
So as you can see in the example below, the testing is not running.
I've even tried to replace the order and run the test first, but then the lint script doesn't run.
Any ideas?
"scripts": {
"prestart": "npm run lint:watch && npm run test:watch",
"start": "open:src",
"open:src": "nodemon --watch server --exec babel-node --debug=5858 --inspect server/srcServer.js --delay 2",
"lint": "node_modules/.bin/esw webpack.config.* --cache --max-warnings 0 src server",
"lint:watch": "npm run lint -- --watch",
"test": "mocha --reporter progress server/testSetup.js 'src/**/*.test.js'",
"test:watch": "npm run test -- --watch"
},
Now I know how it feels to be dumb :)
The "watch" in the test and lint caused it to stop since it doesn't exit.
Thank me very much for solving my issue.

How to use Jest with React Native

The testing section of the docs for React Native suggest that Jest is the official way to do unit tests. However, they don't explain how to get it setup. Running Jest without any setup gives syntax errors (with no line numbers :( ) because it doesn't apply the transforms (eg ES6 features, JSX and Flow) that React Native code tends to use. There's a jestSupport folder in the React Native source try that contains a env.js and scriptPrerocess.js, the latter has code for apply the transforms. So I've copied those into my project and added the following section to my package.json:
"jest": {
"scriptPreprocessor": "jestSupport/scriptPreprocess.js",
"setupEnvScriptFile": "jestSupport/env.js"
},
This fixes the first error but I still get the following error:
Using Jest CLI v0.4.0
FAIL js/sync/__tests__/SynchronisedStorage-tests.js
SyntaxError: /Users/tom/my-project/js/sync/__tests__/SynchronisedStorage-tests.js: /Users/tom/my-project/js/sync/SynchronisedStorage.js: /Users/tom/my-project/node_modules/react-native/Libraries/react-native/react-native.js: /Users/tom/my-project/node_modules/react-native/node_modules/react-tools/src/browser/ui/React.js: /Users/tom/my-project/node_modules/react-native/node_modules/react-tools/src/utils/ReactChildren.js: /Users/tom/my-project/node_modules/react-native/node_modules/react-tools/src/addons/ReactFragment.js: /Users/tom/my-project/node_modules/react-native/node_modules/react-tools/src/classic/element/ReactElement.js: /Users/tom/my-project/node_modules/react-native/node_modules/react-tools/src/core/ReactContext.js: /Users/tom/my-project/node_modules/react-native/node_modules/react-tools/src/vendor/core/warning.js: Unexpected token .
1 test failed, 0 tests passed (1 total)
Run time: 0.456s
Is there more I need to do to make Jest understand React Native? Are there any examples of Jest setup to test React Native?
EDIT:
There was a check in scriptPreprocess that stopped it from running over any file in node_modules which of course included the whole of React Native. Removing that fixes the error above. However, I'm now getting more errors from the React Native source, it definitely seems like it isn't meant to be run within Jest.
EDIT2:
Explicitly setting the mock for the react-native module works:
jest.setMock('react-native', {});
That seems like it's going to be very manual and not very useful for testing code that interacts with the React Native API a lot. I definitely still feel like I'm missing something!
I got Jest to work for my React Native application, and it is running tests without any problem on files with ES6 and ES7 transforms.
To get Jest to work, I followed these steps:
Copied the jestSupport folder from the React Native master repo to my react-native folder in node_modules.
Edited the "jest" line in my packages.json to points to the files in the jestSupport folder
The "jest" line in my packages.json now looks lke this:
"jest": {
"scriptPreprocessor": "<rootDir>/node_modules/react-native/jestSupport/scriptPreprocess.js",
"setupEnvScriptFile": "<rootDir>/node_modules/react-native/jestSupport/env.js",
"testFileExtensions": [
"js"
],
"unmockedModulePathPatterns": [
"source-map"
]
},
As of React Native v0.37.0, Jest is part of the new app template.
In a new app you can run tests right away:
$ react-native init MyAwesomeApp
$ cd MyAwesomeApp
$ npm test
...
Tests: 2 passed
For newer versions of jest, setting it up for react-native is very easy.
Install jest using
npm install --save-dev jest-cli babel-jest
In package.json, add this
"scripts": {
"start": "babel-node ./server/server.js",
"import-data": "babel-node ./scripts/import-data-from-parse.js",
"update-schema": "babel-node ./server/schema/updateSchema.js",
"test": "jest",
"lint": "eslint ."
},
"jest": {
"haste": {
"defaultPlatform": "ios",
"platforms": [
"ios",
"android"
],
"providesModuleNodeModules": [
"react-native"
]
}
},
You might only need to add lines related to jest
Now you can use
npm test
to run jest.
Refer to f8app's github repo to find more about this.