I'm trying to run jest for ESM tests in yarn pnp monorepo,
// list-staged.config.js
export default {
'**/*.js': [
'eslint',
'yarn run staged',
'git add',
]
}
staged script command in my root package.json,
"script": {
"staged": "yarn node --experimental-vm-modules $(yarn bin jest)",
...
}
When I try to commit the code in file /example-project/pages/CheckoutPage.js, lint-staged should just run all the tests in the monorepo but I get this error (note that I have around 84 microservices in the monorepo),
No tests found, exiting with code 1 Run with
--passWithNoTeststo exit with code 0 In /Users/Saud/git/monorepo 5641 files checked across 84 projects. Run with--verbosefor more details. Pattern: /Users/Saud/git/monorepo/example-project/pages/CheckoutPage.js - 0 matches
It's interesting that it's doing a match with the file changed while I haven't specify any options like --onlyChanged or --findRelatedTests in the command.
Root level jest.config.js:
import collectCoverageFrom from './jest.collectCoverageFrom.js'
export default {
collectCoverageFrom,
coverageReporters: ['lcov'],
modulePathIgnorePatterns: [
'<rootDir>/example-project',
],
projects: [
'<rootDir>/example-project',
],
testEnvironment: 'jsdom',
}
example-project/jest.config.js:
import { readFile } from 'fs/promises'
import excludeCoverageFrom from '@monorepo/root/jest.excludeCoverageFrom.js'
export default {
collectCoverageFrom: [
'<rootDir>/example-project/**/*.js',
...excludeCoverageFrom,
],
displayName: 'example-project',
rootDir: '../',
roots: ['<rootDir>/example-project'],
setupFilesAfterEnv: [
// '<rootDir>/jest.window.setImmediate.js',
'<rootDir>/jest.window.scrollTo.js',
'<rootDir>/jest.window.TextEncoder.js',
'@testing-library/jest-dom/extend-expect',
],
testEnvironment: 'jsdom',
moduleNameMapper: {
'\\.(jpg|jpeg|png|svg|webp)$': '<rootDir>/__mocks__/mockImage.js',
},
testPathIgnorePatterns: [
'/cdk.out/',
'/assets-.*/',
'/bundles-.*/',
'/lambda-.*/',
],
coverageThreshold: {
global: {
branches: 50,
functions: 50,
lines: 50,
statements: 50,
},
},
transformIgnorePatterns: ['node_modules/(?!uuid)'],
}
When I run the script itself, the test works fine. For some reason lint-staged is unable to process the yarn command to run jest tests with ESM support. My requirement is to run relevant tests on commits via passing options such as "--findRelatedTests" but for now I'm just trying to make it work. Anyone had this problem?
Other insights, I tried using the flag --findRelatedTests, these are the following commit logs from sourcetree,
[STARTED] Preparing lint-staged...
[SUCCESS] Preparing lint-staged...
[STARTED] Running tasks for staged files...
[STARTED] lint-staged.config.js — 1 file
[STARTED] **/*.js — 1 file
[STARTED] eslint
[SUCCESS] eslint
[STARTED] yarn run staged
[SUCCESS] yarn run staged
[STARTED] git add
[SUCCESS] git add
[SUCCESS] **/*.js — 1 file
[SUCCESS] lint-staged.config.js — 1 file
[SUCCESS] Running tasks for staged files...
[STARTED] Applying modifications from tasks...
[SUCCESS] Applying modifications from tasks...
[STARTED] Cleaning up temporary files...
[SUCCESS] Cleaning up temporary files...
Completed successfully
So for some reason it didn't run any test. I should mention that test file does exists for CheckoutPage.js in /example-project/pages/tests/CheckoutPage.test.js.
So I was using lint-staged incorrectly. I was trying to run all the tests in the monorepo when one file changed. As @jonrsharpe made me realized I didn't understand lint-staged properly. It's a tool for checking files that are going to be committed, so it only makes to run those tests whose files have changed via passing the "--findRelatedTests"