-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Less Modules in react-scripts test (#83)
We need to modify the jest config so that the Less Modules can be tested properly. https://jestjs.io/docs/webpack#mocking-css-modules ```diff const jestConfig = { moduleNameMapper: { - "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy", + "^.+\\.module\\.(css|less|sass|scss)$": "identity-obj-proxy", }, transformIgnorePatterns: [ - "^.+\\.module\\.(css|sass|scss)$", + "^.+\\.module\\.(css|less|sass|scss)$", ], }; ``` Co-authored-by: Kamron Batman <[email protected]>
- Loading branch information
1 parent
129d630
commit 2d4b5ab
Showing
5 changed files
with
146 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ yarn-error.log | |
.vscode | ||
coverage | ||
lib/*.test.js | ||
lib/test-utils.js | ||
yarn.lock | ||
craco-less-*.tgz | ||
.github/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const { createJestConfig } = require("@craco/craco"); | ||
const { processCracoConfig } = require("@craco/craco/lib/config"); | ||
const { applyJestConfigPlugins } = require("@craco/craco/lib/features/plugins"); | ||
const clone = require("clone"); | ||
const CracoLessPlugin = require("./craco-less"); | ||
const { getCracoContext } = require("./test-utils"); | ||
|
||
process.env.NODE_ENV = "test"; | ||
|
||
const baseCracoConfig = {}; | ||
const cracoContext = getCracoContext(baseCracoConfig); | ||
const originalJestConfig = createJestConfig(baseCracoConfig); | ||
|
||
const overrideJestConfig = (callerCracoConfig, jestConfig) => { | ||
return applyJestConfigPlugins( | ||
processCracoConfig({ | ||
...baseCracoConfig, | ||
...callerCracoConfig, | ||
}), | ||
jestConfig, | ||
cracoContext | ||
); | ||
}; | ||
|
||
let jestConfig; | ||
beforeEach(() => { | ||
// deep clone the object before each test. | ||
jestConfig = clone(originalJestConfig); | ||
}); | ||
|
||
test("the jest config is modified correctly", () => { | ||
jestConfig = overrideJestConfig( | ||
{ | ||
plugins: [{ plugin: CracoLessPlugin }], | ||
}, | ||
jestConfig | ||
); | ||
|
||
const moduleNameMapper = jestConfig.moduleNameMapper; | ||
expect(moduleNameMapper["^.+\\.module\\.(css|sass|scss)$"]).toBeUndefined(); | ||
expect(moduleNameMapper["^.+\\.module\\.(css|less|sass|scss)$"]).toEqual( | ||
"identity-obj-proxy" | ||
); | ||
|
||
const transformIgnorePatterns = jestConfig.transformIgnorePatterns; | ||
expect(transformIgnorePatterns[1]).toEqual( | ||
"^.+\\.module\\.(css|less|sass|scss)$" | ||
); | ||
}); | ||
|
||
test("throws an error when we can't find CSS Modules pattern under moduleNameMapper in the jest config", () => { | ||
delete jestConfig.moduleNameMapper["^.+\\.module\\.(css|sass|scss)$"]; | ||
|
||
const runTest = () => { | ||
overrideJestConfig( | ||
{ | ||
plugins: [{ plugin: CracoLessPlugin }], | ||
}, | ||
jestConfig | ||
); | ||
}; | ||
|
||
expect(runTest).toThrowError( | ||
/^Can't find CSS Modules pattern under moduleNameMapper in the test jest config!/ | ||
); | ||
}); | ||
|
||
test("throws an error when we can't find CSS Modules pattern under transformIgnorePatterns in the jest config", () => { | ||
jestConfig.transformIgnorePatterns = | ||
jestConfig.transformIgnorePatterns.filter( | ||
(e) => e !== "^.+\\.module\\.(css|sass|scss)$" | ||
); | ||
|
||
const runTest = () => { | ||
overrideJestConfig( | ||
{ | ||
plugins: [{ plugin: CracoLessPlugin }], | ||
}, | ||
jestConfig | ||
); | ||
}; | ||
|
||
expect(runTest).toThrowError( | ||
/^Can't find CSS Modules pattern under transformIgnorePatterns in the test jest config!/ | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { processCracoConfig } = require("@craco/craco/lib/config"); | ||
const { getCraPaths } = require("@craco/craco/lib/cra"); | ||
|
||
const getCracoContext = (callerCracoConfig, env = process.env.NODE_ENV) => { | ||
const context = { env }; | ||
const cracoConfig = processCracoConfig(callerCracoConfig, { env }); | ||
context.paths = getCraPaths(cracoConfig); | ||
return context; | ||
}; | ||
|
||
module.exports = { | ||
getCracoContext, | ||
}; |