|
| 1 | +const { createJestConfig } = require("@craco/craco"); |
| 2 | +const { processCracoConfig } = require("@craco/craco/lib/config"); |
| 3 | +const { applyJestConfigPlugins } = require("@craco/craco/lib/features/plugins"); |
| 4 | +const clone = require("clone"); |
| 5 | +const CracoLessPlugin = require("./craco-less"); |
| 6 | +const { getCracoContext } = require("./test-utils"); |
| 7 | + |
| 8 | +process.env.NODE_ENV = "test"; |
| 9 | + |
| 10 | +const baseCracoConfig = {}; |
| 11 | +const cracoContext = getCracoContext(baseCracoConfig); |
| 12 | +const originalJestConfig = createJestConfig(baseCracoConfig); |
| 13 | + |
| 14 | +const overrideJestConfig = (callerCracoConfig, jestConfig) => { |
| 15 | + return applyJestConfigPlugins( |
| 16 | + processCracoConfig({ |
| 17 | + ...baseCracoConfig, |
| 18 | + ...callerCracoConfig, |
| 19 | + }), |
| 20 | + jestConfig, |
| 21 | + cracoContext |
| 22 | + ); |
| 23 | +}; |
| 24 | + |
| 25 | +let jestConfig; |
| 26 | +beforeEach(() => { |
| 27 | + // deep clone the object before each test. |
| 28 | + jestConfig = clone(originalJestConfig); |
| 29 | +}); |
| 30 | + |
| 31 | +test("the jest config is modified correctly", () => { |
| 32 | + jestConfig = overrideJestConfig( |
| 33 | + { |
| 34 | + plugins: [{ plugin: CracoLessPlugin }], |
| 35 | + }, |
| 36 | + jestConfig |
| 37 | + ); |
| 38 | + |
| 39 | + const moduleNameMapper = jestConfig.moduleNameMapper; |
| 40 | + expect(moduleNameMapper["^.+\\.module\\.(css|sass|scss)$"]).toBeUndefined(); |
| 41 | + expect(moduleNameMapper["^.+\\.module\\.(css|less|sass|scss)$"]).toEqual( |
| 42 | + "identity-obj-proxy" |
| 43 | + ); |
| 44 | + |
| 45 | + const transformIgnorePatterns = jestConfig.transformIgnorePatterns; |
| 46 | + expect(transformIgnorePatterns[1]).toEqual( |
| 47 | + "^.+\\.module\\.(css|less|sass|scss)$" |
| 48 | + ); |
| 49 | +}); |
| 50 | + |
| 51 | +test("throws an error when we can't find CSS Modules pattern under moduleNameMapper in the jest config", () => { |
| 52 | + delete jestConfig.moduleNameMapper["^.+\\.module\\.(css|sass|scss)$"]; |
| 53 | + |
| 54 | + const runTest = () => { |
| 55 | + overrideJestConfig( |
| 56 | + { |
| 57 | + plugins: [{ plugin: CracoLessPlugin }], |
| 58 | + }, |
| 59 | + jestConfig |
| 60 | + ); |
| 61 | + }; |
| 62 | + |
| 63 | + expect(runTest).toThrowError( |
| 64 | + "Can't find CSS Modules pattern under moduleNameMapper in the test jest config!\n\n" + |
| 65 | + "This error probably occurred because you updated react-scripts or craco. " + |
| 66 | + "Please try updating craco-less to the latest version:\n\n" + |
| 67 | + " $ yarn upgrade craco-less\n\n" + |
| 68 | + "Or:\n\n" + |
| 69 | + " $ npm update craco-less\n\n" + |
| 70 | + "If that doesn't work, craco-less needs to be fixed to support the latest version.\n" + |
| 71 | + "Please check to see if there's already an issue in the DocSpring/craco-less repo:\n\n" + |
| 72 | + " * https://github.com/DocSpring/craco-less/issues?q=is%3Aissue+jest+moduleNameMapper+css\n\n" + |
| 73 | + "If not, please open an issue and we'll take a look. (Or you can send a PR!)\n\n" + |
| 74 | + "You might also want to look for related issues in the " + |
| 75 | + "craco and create-react-app repos:\n\n" + |
| 76 | + " * https://github.com/sharegate/craco/issues?q=is%3Aissue+jest+moduleNameMapper+css\n" + |
| 77 | + " * https://github.com/facebook/create-react-app/issues?q=is%3Aissue+jest+moduleNameMapper+css\n" |
| 78 | + ); |
| 79 | +}); |
| 80 | + |
| 81 | +test("throws an error when we can't find CSS Modules pattern under transformIgnorePatterns in the jest config", () => { |
| 82 | + jestConfig.transformIgnorePatterns = |
| 83 | + jestConfig.transformIgnorePatterns.filter( |
| 84 | + (e) => e !== "^.+\\.module\\.(css|sass|scss)$" |
| 85 | + ); |
| 86 | + |
| 87 | + const runTest = () => { |
| 88 | + overrideJestConfig( |
| 89 | + { |
| 90 | + plugins: [{ plugin: CracoLessPlugin }], |
| 91 | + }, |
| 92 | + jestConfig |
| 93 | + ); |
| 94 | + }; |
| 95 | + |
| 96 | + expect(runTest).toThrowError( |
| 97 | + "Can't find CSS Modules pattern under transformIgnorePatterns in the test jest config!\n\n" + |
| 98 | + "This error probably occurred because you updated react-scripts or craco. " + |
| 99 | + "Please try updating craco-less to the latest version:\n\n" + |
| 100 | + " $ yarn upgrade craco-less\n\n" + |
| 101 | + "Or:\n\n" + |
| 102 | + " $ npm update craco-less\n\n" + |
| 103 | + "If that doesn't work, craco-less needs to be fixed to support the latest version.\n" + |
| 104 | + "Please check to see if there's already an issue in the DocSpring/craco-less repo:\n\n" + |
| 105 | + " * https://github.com/DocSpring/craco-less/issues?q=is%3Aissue+jest+transformIgnorePatterns+css\n\n" + |
| 106 | + "If not, please open an issue and we'll take a look. (Or you can send a PR!)\n\n" + |
| 107 | + "You might also want to look for related issues in the " + |
| 108 | + "craco and create-react-app repos:\n\n" + |
| 109 | + " * https://github.com/sharegate/craco/issues?q=is%3Aissue+jest+transformIgnorePatterns+css\n" + |
| 110 | + " * https://github.com/facebook/create-react-app/issues?q=is%3Aissue+jest+transformIgnorePatterns+css\n" |
| 111 | + ); |
| 112 | +}); |
0 commit comments