Skip to content

Commit 2514e16

Browse files
committed
test: add test for switching implementation with modern-compiler API
1 parent e75dbde commit 2514e16

File tree

3 files changed

+84
-63
lines changed

3 files changed

+84
-63
lines changed

test/__snapshots__/implementation-option.test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ exports[`implementation option not specify with modern API: errors 1`] = `[]`;
4040

4141
exports[`implementation option not specify with modern API: warnings 1`] = `[]`;
4242

43+
exports[`implementation option not specify with modern-compiler API: errors 1`] = `[]`;
44+
45+
exports[`implementation option not specify with modern-compiler API: warnings 1`] = `[]`;
46+
4347
exports[`implementation option not specify: errors 1`] = `[]`;
4448

4549
exports[`implementation option not specify: warnings 1`] = `[]`;
@@ -53,6 +57,14 @@ Some error",
5357

5458
exports[`implementation option should not swallow an error when trying to load a sass implementation: warnings 1`] = `[]`;
5559

60+
exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: errors 1`] = `[]`;
61+
62+
exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: errors 2`] = `[]`;
63+
64+
exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: warnings 1`] = `[]`;
65+
66+
exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: warnings 2`] = `[]`;
67+
5668
exports[`implementation option should throw an error on an unknown sass implementation: errors 1`] = `
5769
[
5870
"ModuleBuildError: Module build failed (from ../src/cjs.js):

test/helpers/getImplementationByName.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ function getImplementationByName(implementationName) {
55
} else if (implementationName === "dart-sass") {
66
// eslint-disable-next-line global-require
77
return require("sass");
8+
} else if (implementationName === "sass-embedded") {
9+
// eslint-disable-next-line global-require
10+
return require("sass-embedded");
811
} else if (implementationName === "sass_string") {
912
return require.resolve("sass");
1013
}

test/implementation-option.test.js

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,34 @@ const spyOnCompiler = (implementation) => {
3131
return compiler;
3232
});
3333

34-
const mockRestore = () => initSpy.mockRestore();
35-
3634
const spies = {
3735
initSpy,
38-
mockRestore,
36+
mockClear() {
37+
if (this.compileStringSpy) {
38+
this.compileStringSpy.mockClear();
39+
}
40+
},
41+
mockRestore() {
42+
initSpy.mockRestore();
43+
delete this.compileStringSpy;
44+
},
3945
};
4046

4147
return spies;
4248
};
4349

4450
describe("implementation option", () => {
51+
const nodeSassSpy = jest.spyOn(nodeSass, "render");
52+
const dartSassSpy = jest.spyOn(dartSass, "render");
53+
const dartSassSpyModernAPI = jest.spyOn(dartSass, "compileStringAsync");
54+
const dartSassCompilerSpies = spyOnCompiler(dartSass);
55+
const sassEmbeddedSpy = jest.spyOn(sassEmbedded, "render");
56+
const sassEmbeddedSpyModernAPI = jest.spyOn(
57+
sassEmbedded,
58+
"compileStringAsync",
59+
);
60+
const sassEmbeddedCompilerSpies = spyOnCompiler(sassEmbedded);
61+
4562
implementations.forEach((item) => {
4663
let implementationName;
4764
let implementation;
@@ -56,17 +73,6 @@ describe("implementation option", () => {
5673
}
5774

5875
it(`'${implementationName}', '${api}' API`, async () => {
59-
const nodeSassSpy = jest.spyOn(nodeSass, "render");
60-
const dartSassSpy = jest.spyOn(dartSass, "render");
61-
const dartSassSpyModernAPI = jest.spyOn(dartSass, "compileStringAsync");
62-
const dartSassCompilerSpies = spyOnCompiler(dartSass);
63-
const sassEmbeddedSpy = jest.spyOn(sassEmbedded, "render");
64-
const sassEmbeddedSpyModernAPI = jest.spyOn(
65-
sassEmbedded,
66-
"compileStringAsync",
67-
);
68-
const sassEmbeddedCompilerSpies = spyOnCompiler(sassEmbedded);
69-
7076
const testId = getTestId("language", "scss");
7177
const options = { api, implementation };
7278
const compiler = getCompiler(testId, { loader: { options } });
@@ -83,10 +89,8 @@ describe("implementation option", () => {
8389
expect(nodeSassSpy).toHaveBeenCalledTimes(1);
8490
expect(dartSassSpy).toHaveBeenCalledTimes(0);
8591
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
86-
expect(dartSassCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
8792
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(0);
8893
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(0);
89-
expect(sassEmbeddedCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
9094
} else if (
9195
implementationName === "dart-sass" ||
9296
implementationName === "dart-sass_string"
@@ -95,68 +99,56 @@ describe("implementation option", () => {
9599
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
96100
expect(dartSassSpy).toHaveBeenCalledTimes(0);
97101
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(1);
98-
expect(dartSassCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
99102
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(0);
100103
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(0);
101-
expect(sassEmbeddedCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
102104
} else if (api === "modern-compiler") {
103105
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
104106
expect(dartSassSpy).toHaveBeenCalledTimes(0);
105107
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
106-
expect(dartSassCompilerSpies.initSpy).toHaveBeenCalledTimes(1);
107108
expect(dartSassCompilerSpies.compileStringSpy).toHaveBeenCalledTimes(
108109
1,
109110
);
110111
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(0);
111112
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(0);
112-
expect(sassEmbeddedCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
113113
} else if (api === "legacy") {
114114
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
115115
expect(dartSassSpy).toHaveBeenCalledTimes(1);
116116
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
117-
expect(dartSassCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
118117
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(0);
119118
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(0);
120-
expect(sassEmbeddedCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
121119
}
122120
} else if (implementationName === "sass-embedded") {
123121
if (api === "modern") {
124122
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
125123
expect(dartSassSpy).toHaveBeenCalledTimes(0);
126124
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
127-
expect(dartSassCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
128125
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(0);
129126
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(1);
130-
expect(sassEmbeddedCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
131127
} else if (api === "modern-compiler") {
132128
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
133129
expect(dartSassSpy).toHaveBeenCalledTimes(0);
134130
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
135-
expect(dartSassCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
136131
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(0);
137132
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(0);
138-
expect(sassEmbeddedCompilerSpies.initSpy).toHaveBeenCalledTimes(1);
139133
expect(
140134
sassEmbeddedCompilerSpies.compileStringSpy,
141135
).toHaveBeenCalledTimes(1);
142136
} else if (api === "legacy") {
143137
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
144138
expect(dartSassSpy).toHaveBeenCalledTimes(0);
145139
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
146-
expect(dartSassCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
147140
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(1);
148141
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(0);
149-
expect(sassEmbeddedCompilerSpies.initSpy).toHaveBeenCalledTimes(0);
150142
}
151143
}
152144

153-
nodeSassSpy.mockRestore();
154-
dartSassSpy.mockRestore();
155-
dartSassSpyModernAPI.mockRestore();
156-
dartSassCompilerSpies.mockRestore();
157-
sassEmbeddedSpy.mockRestore();
158-
sassEmbeddedSpyModernAPI.mockRestore();
159-
sassEmbeddedCompilerSpies.mockRestore();
145+
nodeSassSpy.mockClear();
146+
dartSassSpy.mockClear();
147+
dartSassSpyModernAPI.mockClear();
148+
dartSassCompilerSpies.mockClear();
149+
sassEmbeddedSpy.mockClear();
150+
sassEmbeddedSpyModernAPI.mockClear();
151+
sassEmbeddedCompilerSpies.mockClear();
160152
});
161153
});
162154

@@ -173,9 +165,6 @@ describe("implementation option", () => {
173165
});
174166

175167
it("not specify", async () => {
176-
const nodeSassSpy = jest.spyOn(nodeSass, "render");
177-
const dartSassSpy = jest.spyOn(dartSass, "render");
178-
179168
const testId = getTestId("language", "scss");
180169
const options = {};
181170
const compiler = getCompiler(testId, { loader: { options } });
@@ -191,14 +180,11 @@ describe("implementation option", () => {
191180
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
192181
expect(dartSassSpy).toHaveBeenCalledTimes(1);
193182

194-
nodeSassSpy.mockRestore();
195-
dartSassSpy.mockRestore();
183+
nodeSassSpy.mockClear();
184+
dartSassSpy.mockClear();
196185
});
197186

198187
it("not specify with legacy API", async () => {
199-
const nodeSassSpy = jest.spyOn(nodeSass, "render");
200-
const dartSassSpy = jest.spyOn(dartSass, "render");
201-
202188
const testId = getTestId("language", "scss");
203189
const options = {
204190
api: "legacy",
@@ -216,14 +202,11 @@ describe("implementation option", () => {
216202
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
217203
expect(dartSassSpy).toHaveBeenCalledTimes(1);
218204

219-
nodeSassSpy.mockRestore();
220-
dartSassSpy.mockRestore();
205+
nodeSassSpy.mockClear();
206+
dartSassSpy.mockClear();
221207
});
222208

223209
it("not specify with modern API", async () => {
224-
const nodeSassSpy = jest.spyOn(nodeSass, "render");
225-
const dartSassSpy = jest.spyOn(dartSass, "compileStringAsync");
226-
227210
const testId = getTestId("language", "scss");
228211
const options = {
229212
api: "modern",
@@ -239,17 +222,13 @@ describe("implementation option", () => {
239222
expect(getErrors(stats)).toMatchSnapshot("errors");
240223

241224
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
242-
expect(dartSassSpy).toHaveBeenCalledTimes(1);
225+
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(1);
243226

244-
nodeSassSpy.mockRestore();
245-
dartSassSpy.mockRestore();
227+
nodeSassSpy.mockClear();
228+
dartSassSpyModernAPI.mockClear();
246229
});
247230

248-
it.skip("not specify with modern-compiler API", async () => {
249-
const nodeSassSpy = jest.spyOn(nodeSass, "render");
250-
const dartSassSpy = jest.spyOn(dartSass, "compileStringAsync");
251-
const dartSassCompilerSpies = spyOnCompiler(dartSass);
252-
231+
it("not specify with modern-compiler API", async () => {
253232
const testId = getTestId("language", "scss");
254233
const options = {
255234
api: "modern-compiler",
@@ -265,17 +244,44 @@ describe("implementation option", () => {
265244
expect(getErrors(stats)).toMatchSnapshot("errors");
266245

267246
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
268-
expect(dartSassSpy).toHaveBeenCalledTimes(0);
269-
// TODO: this isn't being called because the compiler is already initialized
270-
// from a previous test.
271-
expect(dartSassCompilerSpies.initSpy).toHaveBeenCalledTimes(1);
247+
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
272248
expect(dartSassCompilerSpies.compileStringSpy).toHaveBeenCalledTimes(1);
273249

274-
nodeSassSpy.mockRestore();
275-
dartSassSpy.mockRestore();
276-
dartSassCompilerSpies.mockRestore();
250+
nodeSassSpy.mockClear();
251+
dartSassSpyModernAPI.mockClear();
252+
dartSassCompilerSpies.mockClear();
277253
});
278254

255+
it.each(["dart-sass", "sass-embedded"])(
256+
"should support switching the implementation within the same process when using the modern-compiler API",
257+
async (implementationName) => {
258+
const testId = getTestId("language", "scss");
259+
const options = {
260+
api: "modern-compiler",
261+
implementation: getImplementationByName(implementationName),
262+
};
263+
const compiler = getCompiler(testId, { loader: { options } });
264+
const stats = await compile(compiler);
265+
const { css, sourceMap } = getCodeFromBundle(stats, compiler);
266+
267+
expect(css).toBeDefined();
268+
expect(sourceMap).toBeUndefined();
269+
270+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
271+
expect(getErrors(stats)).toMatchSnapshot("errors");
272+
273+
expect(dartSassCompilerSpies.compileStringSpy).toHaveBeenCalledTimes(
274+
implementationName === "dart-sass" ? 1 : 0,
275+
);
276+
expect(sassEmbeddedCompilerSpies.compileStringSpy).toHaveBeenCalledTimes(
277+
implementationName === "sass-embedded" ? 1 : 0,
278+
);
279+
280+
dartSassCompilerSpies.mockClear();
281+
sassEmbeddedCompilerSpies.mockClear();
282+
},
283+
);
284+
279285
it("should throw an error on an unknown sass implementation", async () => {
280286
const testId = getTestId("language", "scss");
281287
const options = {

0 commit comments

Comments
 (0)