Skip to content

Commit

Permalink
test: added
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 19, 2024
1 parent 23be8db commit 8efac43
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/configCases/css/contenthash/async.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: yellow;
color: green;
}
1 change: 1 addition & 0 deletions test/configCases/css/contenthash/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = 'async';
28 changes: 28 additions & 0 deletions test/configCases/css/contenthash/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as style from "./style.css";

it("should work with js", done => {
import('./async.js').then(x => {
expect(x.name).toBe("async")
done();
}, done);
});

it("should work with css", done => {
expect(style).toEqual(nsObj({}));

const computedStyle = getComputedStyle(document.body);

expect(computedStyle.getPropertyValue("background")).toBe(" green");
expect(computedStyle.getPropertyValue("color")).toBe(" yellow");

import("./async.css").then(x => {
expect(x).toEqual(nsObj({}));

const style = getComputedStyle(document.body);

expect(style.getPropertyValue("background")).toBe(" yellow");
expect(style.getPropertyValue("color")).toBe(" green");

done();
}, done);
});
4 changes: 4 additions & 0 deletions test/configCases/css/contenthash/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: green;
color: yellow;
}
49 changes: 49 additions & 0 deletions test/configCases/css/contenthash/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fs = require("fs");

let cssBundle;

module.exports = {
findBundle: function (_, options) {
const jsBundleRegex = new RegExp(/^bundle\..+\.js$/, "i");
const cssBundleRegex = new RegExp(/^bundle\..+\.css$/, "i");
const asyncRegex = new RegExp(/^async\..+\.js$/, "i");
const files = fs.readdirSync(options.output.path);
const jsBundle = files.find(file => jsBundleRegex.test(file));

if (!jsBundle) {
throw new Error(
`No file found with correct name (regex: ${
jsBundleRegex.source
}, files: ${files.join(", ")})`
);
}

const async = files.find(file => asyncRegex.test(file));

if (!async) {
throw new Error(
`No file found with correct name (regex: ${
asyncRegex.source
}, files: ${files.join(", ")})`
);
}

cssBundle = files.find(file => cssBundleRegex.test(file));

if (!cssBundle) {
throw new Error(
`No file found with correct name (regex: ${
cssBundleRegex.source
}, files: ${files.join(", ")})`
);
}

return [jsBundle, async];
},
moduleScope(scope) {
const link = scope.window.document.createElement("link");
link.rel = "stylesheet";
link.href = cssBundle;
scope.window.document.head.appendChild(link);
}
};
14 changes: 14 additions & 0 deletions test/configCases/css/contenthash/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
target: "web",
mode: "development",
output: {
filename: "bundle.[name].[contenthash].js",
cssFilename: "bundle.[name].[contenthash].css",
chunkFilename: "async.[name].[contenthash].js",
cssChunkFilename: "async.[name].[contenthash].css"
},
experiments: {
css: true
}
};

0 comments on commit 8efac43

Please sign in to comment.