Skip to content

Commit

Permalink
test: improve
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 11, 2024
1 parent 4a00573 commit 3f45fe1
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/utils.js
Expand Up @@ -767,16 +767,16 @@ function getCompileFn(loaderContext, implementation, options) {

// Check again because awaiting the initialization function
// introduces a race condition.
if (!sassModernCompilers.has(implementation)) {
sassModernCompilers.set(implementation, compiler);
if (!sassModernCompilers.has(webpackCompiler)) {
sassModernCompilers.set(webpackCompiler, compiler);
webpackCompiler.hooks.shutdown.tap("sass-loader", () => {
compiler.dispose();
});
}
}

return sassModernCompilers
.get(implementation)
.get(webpackCompiler)
.compileStringAsync(data, rest);
}

Expand Down
9 changes: 9 additions & 0 deletions test/additionalData-option.test.js
@@ -1,5 +1,6 @@
import {
compile,
close,
getCodeFromBundle,
getCodeFromSass,
getCompiler,
Expand Down Expand Up @@ -35,6 +36,8 @@ describe("additionalData option", () => {
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});

it(`should work as a function ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => {
Expand All @@ -60,6 +63,8 @@ describe("additionalData option", () => {
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});

it(`should work as an async function ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => {
Expand All @@ -85,6 +90,8 @@ describe("additionalData option", () => {
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});

it(`should use same EOL on all os ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => {
Expand All @@ -106,6 +113,8 @@ a {
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});
});
});
Expand Down
10 changes: 10 additions & 0 deletions test/helpers/close.js
@@ -0,0 +1,10 @@
export default (compiler) =>
new Promise((resolve, reject) => {
compiler.close((error) => {
if (error) {
return reject(error);
}

return resolve();
});
});
2 changes: 2 additions & 0 deletions test/helpers/index.js
@@ -1,4 +1,5 @@
import compile from "./compiler";
import close from "./close";
import customFunctions from "./customFunctions";
import customImporter from "./customImporter";
import getCodeFromBundle from "./getCodeFromBundle";
Expand All @@ -13,6 +14,7 @@ import readAsset from "./readAsset";

export {
compile,
close,
customFunctions,
customImporter,
getCodeFromBundle,
Expand Down
25 changes: 25 additions & 0 deletions test/implementation-option.test.js
Expand Up @@ -4,6 +4,7 @@ import dartSass from "sass";
import * as sassEmbedded from "sass-embedded";

import {
close,
compile,
getCodeFromBundle,
getCompiler,
Expand Down Expand Up @@ -149,6 +150,8 @@ describe("implementation option", () => {
sassEmbeddedSpy.mockClear();
sassEmbeddedSpyModernAPI.mockClear();
sassEmbeddedCompilerSpies.mockClear();

await close(compiler);
});
});

Expand All @@ -162,6 +165,8 @@ describe("implementation option", () => {

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});

it("not specify", async () => {
Expand All @@ -182,6 +187,8 @@ describe("implementation option", () => {

nodeSassSpy.mockClear();
dartSassSpy.mockClear();

await close(compiler);
});

it("not specify with legacy API", async () => {
Expand All @@ -204,6 +211,8 @@ describe("implementation option", () => {

nodeSassSpy.mockClear();
dartSassSpy.mockClear();

await close(compiler);
});

it("not specify with modern API", async () => {
Expand All @@ -226,6 +235,8 @@ describe("implementation option", () => {

nodeSassSpy.mockClear();
dartSassSpyModernAPI.mockClear();

await close(compiler);
});

it("not specify with modern-compiler API", async () => {
Expand All @@ -250,6 +261,8 @@ describe("implementation option", () => {
nodeSassSpy.mockClear();
dartSassSpyModernAPI.mockClear();
dartSassCompilerSpies.mockClear();

await close(compiler);
});

it.each(["dart-sass", "sass-embedded"])(
Expand Down Expand Up @@ -279,6 +292,8 @@ describe("implementation option", () => {

dartSassCompilerSpies.mockClear();
sassEmbeddedCompilerSpies.mockClear();

await close(compiler);
},
);

Expand All @@ -295,6 +310,8 @@ describe("implementation option", () => {

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});

it('should throw an error when the "info" is unparseable', async () => {
Expand All @@ -308,6 +325,8 @@ describe("implementation option", () => {

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});

it('should throw error when the "info" does not exist', async () => {
Expand All @@ -322,6 +341,8 @@ describe("implementation option", () => {

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});

it("should try to load using valid order", async () => {
Expand Down Expand Up @@ -360,6 +381,8 @@ describe("implementation option", () => {

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});

it("should not swallow an error when trying to load a sass implementation", async () => {
Expand Down Expand Up @@ -389,5 +412,7 @@ describe("implementation option", () => {

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

await close(compiler);
});
});

0 comments on commit 3f45fe1

Please sign in to comment.