Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test run to use transpiled library and not include styles #41

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions lib/craco-antd.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const {
mergeWebpackConfig
} = require("@craco/craco/lib/features/webpack/merge-webpack-config");
const {
applyCracoConfigPlugins,
applyWebpackConfigPlugins
applyCracoConfigPlugins
} = require("@craco/craco/lib/features/plugins");

const clone = require("clone");
Expand All @@ -25,13 +24,11 @@ beforeEach(() => {
mockReadLess.mockImplementation(() => false);
mockReadJSON.mockImplementation(() => false);

if (!originalWebpackConfig) {
process.env.NODE_ENV = "development";
originalWebpackConfig = loadWebpackDevConfig({
reactScriptsVersion: "react-scripts"
});
process.env.NODE_ENV = "test";
}
process.env.NODE_ENV = "development";

const cracoConfig = { reactScriptsVersion: "react-scripts" };
originalWebpackConfig =
originalWebpackConfig || loadWebpackDevConfig(cracoConfig);
webpackConfig = clone(originalWebpackConfig);
});

Expand Down Expand Up @@ -91,9 +88,9 @@ test("the webpack config is modified correctly with all options and Less vars",
]
});

expect(mockReadLess.mock.calls.length).toBe(1);
expect(mockReadLess.mock.calls[0][0]).toBe(`.${path.sep}antd.customize.less`);
expect(mockReadJSON.mock.calls.length).toBe(0);
expect(mockReadLess).toBeCalledTimes(1);
expect(mockReadLess).toBeCalledWith(`.${path.sep}antd.customize.less`);
expect(mockReadJSON).toBeCalledTimes(0);

const oneOfRules = webpackConfig.module.rules.find(r => r.oneOf);
expect(oneOfRules).not.toBeUndefined();
Expand Down Expand Up @@ -184,10 +181,10 @@ test("the webpack config is modified correctly when loading vars from JSON file"
]
});

expect(mockReadLess.mock.calls.length).toBe(1);
expect(mockReadLess.mock.calls[0][0]).toBe(`.${path.sep}antd.customize.less`);
expect(mockReadJSON.mock.calls.length).toBe(1);
expect(mockReadJSON.mock.calls[0][0]).toBe(`.${path.sep}antd.customize.json`);
expect(mockReadLess).toBeCalledTimes(1);
expect(mockReadLess).toBeCalledWith(`.${path.sep}antd.customize.less`);
expect(mockReadJSON).toBeCalledTimes(1);
expect(mockReadJSON).toBeCalledWith(`.${path.sep}antd.customize.json`);

const oneOfRules = webpackConfig.module.rules.find(r => r.oneOf);
expect(oneOfRules).not.toBeUndefined();
Expand Down Expand Up @@ -254,9 +251,9 @@ test("custom Less variables path", () => {
]
});

expect(mockReadLess.mock.calls.length).toBe(1);
expect(mockReadLess.mock.calls[0][0]).toBe("./src/styles/antd.custom.less");
expect(mockReadJSON.mock.calls.length).toBe(0);
expect(mockReadLess).toBeCalledTimes(1);
expect(mockReadLess).toBeCalledWith("./src/styles/antd.custom.less");
expect(mockReadJSON).toBeCalledTimes(0);
});

test("custom JSON variables path", () => {
Expand All @@ -276,8 +273,8 @@ test("custom JSON variables path", () => {
]
});

expect(mockReadJSON.mock.calls.length).toBe(1);
expect(mockReadJSON.mock.calls[0][0]).toBe("./src/styles/antd.custom.json");
expect(mockReadJSON).toBeCalledTimes(1);
expect(mockReadJSON).toBeCalledWith("./src/styles/antd.custom.json");
});

const runExpectationsForMinimalConfiguration = () => {
Expand Down Expand Up @@ -344,3 +341,22 @@ test("invalid JSON in antd.customize.json", () => {
"SyntaxError: Unexpected token t in JSON at position 2"
);
});

test("when run within a test should use transpiled library and not include styles", () => {
mockReadLess.mockImplementation(() => "");

process.env.NODE_ENV = "test";
applyCracoConfigAndOverrideWebpack({
plugins: [{ plugin: CracoAntDesignPlugin }]
});

const oneOfRules = webpackConfig.module.rules.find(r => r.oneOf);
const jsRule = oneOfRules.oneOf.find(
r => r.test && r.test.toString() === "/\\.(js|mjs|jsx|ts|tsx)$/"
);
expect(jsRule).not.toBeUndefined();
expect(jsRule.options.plugins[1]).toEqual([
"import",
{ libraryName: "antd", libraryDirectory: "lib" }
]);
});
15 changes: 11 additions & 4 deletions lib/craco-antd.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,19 @@ module.exports = {
overrideCracoConfig: ({ cracoConfig }) => {
if (!cracoConfig.babel) cracoConfig.babel = {};
if (!cracoConfig.babel.plugins) cracoConfig.babel.plugins = [];
// Use `style: 'css'` to include the precompiled CSS.
// `style: true` loads the original Less so that variables can be modified.
// See: https://github.com/DocSpring/craco-antd/issues/3
const importConfig =
process.env.NODE_ENV === "test"
? // Use the transpiled library in tests and don't include any styles
// See: https://github.com/DocSpring/craco-antd/issues/10
{ libraryDirectory: "lib" }
: // Use `style: 'css'` to include the precompiled CSS.
// `style: true` loads the original Less so that variables can be modified.
// See: https://github.com/DocSpring/craco-antd/issues/3
{ libraryDirectory: "es", style: true };

cracoConfig.babel.plugins.push([
"import",
{ libraryName: "antd", libraryDirectory: "es", style: true }
Object.assign({ libraryName: "antd" }, importConfig)
]);
return cracoConfig;
}
Expand Down
39 changes: 18 additions & 21 deletions lib/craco-antd.prod.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const {
mergeWebpackConfig
} = require("@craco/craco/lib/features/webpack/merge-webpack-config");
const {
applyCracoConfigPlugins,
applyWebpackConfigPlugins
applyCracoConfigPlugins
} = require("@craco/craco/lib/features/plugins");

const clone = require("clone");
Expand All @@ -25,13 +24,11 @@ beforeEach(() => {
mockReadLess.mockImplementation(() => false);
mockReadJSON.mockImplementation(() => false);

if (!originalWebpackConfig) {
process.env.NODE_ENV = "production";
originalWebpackConfig = loadWebpackProdConfig({
reactScriptsVersion: "react-scripts"
});
process.env.NODE_ENV = "test";
}
process.env.NODE_ENV = "production";

const cracoConfig = { reactScriptsVersion: "react-scripts" };
originalWebpackConfig =
originalWebpackConfig || loadWebpackProdConfig(cracoConfig);
webpackConfig = clone(originalWebpackConfig);
});

Expand Down Expand Up @@ -91,9 +88,9 @@ test("the webpack config is modified correctly with all options and Less vars",
]
});

expect(mockReadLess.mock.calls.length).toBe(1);
expect(mockReadLess.mock.calls[0][0]).toBe(`.${path.sep}antd.customize.less`);
expect(mockReadJSON.mock.calls.length).toBe(0);
expect(mockReadLess).toBeCalledTimes(1);
expect(mockReadLess).toBeCalledWith(`.${path.sep}antd.customize.less`);
expect(mockReadJSON).toBeCalledTimes(0);

const oneOfRules = webpackConfig.module.rules.find(r => r.oneOf);
expect(oneOfRules).not.toBeUndefined();
Expand Down Expand Up @@ -181,10 +178,10 @@ test("the webpack config is modified correctly when loading vars from JSON file"
]
});

expect(mockReadLess.mock.calls.length).toBe(1);
expect(mockReadLess.mock.calls[0][0]).toBe(`.${path.sep}antd.customize.less`);
expect(mockReadJSON.mock.calls.length).toBe(1);
expect(mockReadJSON.mock.calls[0][0]).toBe(`.${path.sep}antd.customize.json`);
expect(mockReadLess).toBeCalledTimes(1);
expect(mockReadLess).toBeCalledWith(`.${path.sep}antd.customize.less`);
expect(mockReadJSON).toBeCalledTimes(1);
expect(mockReadJSON).toBeCalledWith(`.${path.sep}antd.customize.json`);

const oneOfRules = webpackConfig.module.rules.find(r => r.oneOf);
expect(oneOfRules).not.toBeUndefined();
Expand Down Expand Up @@ -248,9 +245,9 @@ test("custom Less variables path", () => {
]
});

expect(mockReadLess.mock.calls.length).toBe(1);
expect(mockReadLess.mock.calls[0][0]).toBe("./src/styles/antd.custom.less");
expect(mockReadJSON.mock.calls.length).toBe(0);
expect(mockReadLess).toBeCalledTimes(1);
expect(mockReadLess).toBeCalledWith("./src/styles/antd.custom.less");
expect(mockReadJSON).toBeCalledTimes(0);
});

test("custom JSON variables path", () => {
Expand All @@ -270,8 +267,8 @@ test("custom JSON variables path", () => {
]
});

expect(mockReadJSON.mock.calls.length).toBe(1);
expect(mockReadJSON.mock.calls[0][0]).toBe("./src/styles/antd.custom.json");
expect(mockReadJSON).toBeCalledTimes(1);
expect(mockReadJSON).toBeCalledWith("./src/styles/antd.custom.json");
});

const runExpectationsForMinimalConfiguration = () => {
Expand Down