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

chore: Add transformer tests #188

Merged
merged 12 commits into from
Mar 19, 2024
5 changes: 5 additions & 0 deletions .changeset/breezy-games-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workleap/webpack-configs": patch
---

Updated dependencies.
16 changes: 15 additions & 1 deletion packages/swc-configs/tests/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test("when parser is \"typescript\", tsx parsing is enabled", () => {
expect((result.jsc?.parser as TsParserConfig).tsx).toBeTruthy();
});

test("when a transformer is provided, the transformer is applied on the swc config", () => {
test("when a transformer is provided, and the transformer update the existing configuration object, the transformer is applied on the swc config", () => {
const minifyTransformer: SwcConfigTransformer = (config: SwcConfig) => {
config.minify = true;

Expand All @@ -58,6 +58,20 @@ test("when a transformer is provided, the transformer is applied on the swc conf
expect(result.minify).toBeTruthy();
});

test("when a transformer is provided, and the transformer returns a new configuration object, the transformer is applied on the swc config", () => {
const minifyTransformer: SwcConfigTransformer = () => {
return {
minify: true
};
};

const result = defineBuildConfig(Targets, {
transformers: [minifyTransformer]
});

expect(result.minify).toBeTruthy();
});

test("when multiple transformers are provided, all the transformers are applied on the swc config", () => {
const minifyTransformer: SwcConfigTransformer = (config: SwcConfig) => {
config.minify = true;
Expand Down
16 changes: 15 additions & 1 deletion packages/swc-configs/tests/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test("when parser is \"typescript\", tsx parsing is enabled", () => {
expect((result.jsc?.parser as TsParserConfig).tsx).toBeTruthy();
});

test("when a transformer is provided, the transformer is applied on the swc config", () => {
test("when a transformer is provided, and the transformer update the existing configuration object, the transformer is applied on the swc config", () => {
const minifyTransformer: SwcConfigTransformer = (config: SwcConfig) => {
config.minify = true;

Expand All @@ -74,6 +74,20 @@ test("when a transformer is provided, the transformer is applied on the swc conf
expect(result.minify).toBeTruthy();
});

test("when a transformer is provided, and the transformer returns a new configuration object, the transformer is applied on the swc config", () => {
const minifyTransformer: SwcConfigTransformer = () => {
return {
minify: true
};
};

const result = defineDevConfig(Targets, {
transformers: [minifyTransformer]
});

expect(result.minify).toBeTruthy();
});

test("when multiple transformers are provided, all the transformers are applied on the swc config", () => {
const minifyTransformer: SwcConfigTransformer = (config: SwcConfig) => {
config.minify = true;
Expand Down
16 changes: 15 additions & 1 deletion packages/swc-configs/tests/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("ecmascript parser", () => {
});
});

test("when a transformer is provided, the transformer is applied on the swc config", () => {
test("when a transformer is provided, and the transformer update the existing configuration object, the transformer is applied on the swc config", () => {
const minifyTransformer: SwcConfigTransformer = (config: SwcConfig) => {
config.minify = true;

Expand All @@ -92,6 +92,20 @@ test("when a transformer is provided, the transformer is applied on the swc conf
expect(result.minify).toBeTruthy();
});

test("when a transformer is provided, and the transformer returns a new configuration object, the transformer is applied on the swc config", () => {
const minifyTransformer: SwcConfigTransformer = () => {
return {
minify: true
};
};

const result = defineJestConfig({
transformers: [minifyTransformer]
});

expect(result.minify).toBeTruthy();
});

test("when multiple transformers are provided, all the transformers are applied on the swc config", () => {
const minifyTransformer: SwcConfigTransformer = (config: SwcConfig) => {
config.minify = true;
Expand Down
16 changes: 15 additions & 1 deletion packages/tsup-configs/tests/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test("when a format array option is provided, do not merge the provided array wi
expect(result.format![0]).toBe("cjs");
});

test("when a transformer is provided, the transformer is applied on the tsup config", () => {
test("when a transformer is provided, and the transformer update the existing configuration object, the transformer is applied on the tsup config", () => {
const platformTransformer: TsupConfigTransformer = (config: Options) => {
config.platform = "node";

Expand All @@ -44,6 +44,20 @@ test("when a transformer is provided, the transformer is applied on the tsup con
expect(result.platform).toBe("node");
});

test("when a transformer is provided, and the transformer returns a new configuration object, the transformer is applied on the tsup config", () => {
const platformTransformer: TsupConfigTransformer = () => {
return {
platform: "node"
};
};

const result = defineBuildConfig({
transformers: [platformTransformer]
});

expect(result.platform).toBe("node");
});

test("when multiple transformers are provided, all the transformers are applied on the webpack config", () => {
const platformTransformer: TsupConfigTransformer = (config: Options) => {
config.platform = "node";
Expand Down
16 changes: 15 additions & 1 deletion packages/tsup-configs/tests/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test("when a format array option is provided, do not merge the provided array wi
expect(result.format![0]).toBe("cjs");
});

test("when a transformer is provided, the transformer is applied on the tsup config", () => {
test("when a transformer is provided, and the transformer update the existing configuration object, the transformer is applied on the tsup config", () => {
const platformTransformer: TsupConfigTransformer = (config: Options) => {
config.platform = "node";

Expand All @@ -44,6 +44,20 @@ test("when a transformer is provided, the transformer is applied on the tsup con
expect(result.platform).toBe("node");
});

test("when a transformer is provided, and the transformer returns a new configuration object, the transformer is applied on the tsup config", () => {
const platformTransformer: TsupConfigTransformer = () => {
return {
platform: "node"
};
};

const result = defineDevConfig({
transformers: [platformTransformer]
});

expect(result.platform).toBe("node");
});

test("when multiple transformers are provided, all the transformers are applied on the webpack config", () => {
const platformTransformer: TsupConfigTransformer = (config: Options) => {
config.platform = "node";
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-configs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"html-webpack-plugin": "^5.6.0",
"mini-css-extract-plugin": "^2.8.1",
"postcss-loader": "^8.1.1",
"react-refresh": "^0.14.0",
"style-loader": "^3.3.4",
"swc-loader": "^0.2.6",
"terser-webpack-plugin": "^5.3.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-configs/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export function defineDevConfig(swcConfig: SwcConfig, options: DefineDevConfigOp
}),
fastRefresh && new ReactRefreshWebpackPlugin(trySetFastRefreshOverlay(isObject(fastRefresh) ? fastRefresh : defineFastRefreshPluginConfig(), overlay)),
...plugins
].filter(Boolean) as WebpackConfig["plugins"]
].filter(Boolean)
};

const transformedConfig = applyTransformers(config, transformers, {
Expand Down
16 changes: 15 additions & 1 deletion packages/webpack-configs/tests/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ test("the provided swc config object is set as the swc-loader options", () => {
expect((swcLoader?.moduleRule as RuleSetRule).options).toBe(swcConfig);
});

test("when a transformer is provided, the transformer is applied on the webpack config", () => {
test("when a transformer is provided, and the transformer update the existing configuration object, the transformer is applied on the webpack config", () => {
const entryTransformer: WebpackConfigTransformer = (config: Configuration) => {
config.entry = "a-custom-value-in-a-transformer";

Expand All @@ -265,6 +265,20 @@ test("when a transformer is provided, the transformer is applied on the webpack
expect(result.entry).toBe("a-custom-value-in-a-transformer");
});

test("when a transformer is provided, and the transformer returns a new configuration object, the transformer is applied on the webpack config", () => {
const entryTransformer: WebpackConfigTransformer = () => {
return {
entry: "a-custom-value-in-a-transformer"
};
};

const result = defineBuildConfig(SwcConfig, {
transformers: [entryTransformer]
});

expect(result.entry).toBe("a-custom-value-in-a-transformer");
});

test("when multiple transformers are provided, all the transformers are applied on the webpack config", () => {
const entryTransformer: WebpackConfigTransformer = (config: Configuration) => {
config.entry = "a-custom-value-in-a-transformer";
Expand Down
16 changes: 15 additions & 1 deletion packages/webpack-configs/tests/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ test("the provided swc config object is set as the swc-loader options", () => {
expect((swcLoader?.moduleRule as RuleSetRule).options).toBe(config);
});

test("when a transformer is provided, the transformer is applied on the webpack config", () => {
test("when a transformer is provided, and the transformer update the existing configuration object, the transformer is applied on the webpack config", () => {
const entryTransformer: WebpackConfigTransformer = (config: Configuration) => {
config.entry = "a-custom-value-in-a-transformer";

Expand All @@ -308,6 +308,20 @@ test("when a transformer is provided, the transformer is applied on the webpack
expect(result.entry).toBe("a-custom-value-in-a-transformer");
});

test("when a transformer is provided, and the transformer returns a new configuration object, the transformer is applied on the webpack config", () => {
const entryTransformer: WebpackConfigTransformer = () => {
return {
entry: "a-custom-value-in-a-transformer"
};
};

const result = defineDevConfig(DefaultConfig, {
transformers: [entryTransformer]
});

expect(result.entry).toBe("a-custom-value-in-a-transformer");
});

test("when multiple transformers are provided, all the transformers are applied on the webpack config", () => {
const entryTransformer: WebpackConfigTransformer = (config: Configuration) => {
config.entry = "a-custom-value-in-a-transformer";
Expand Down
Loading
Loading