Skip to content

Commit

Permalink
Make tests work with Dart Sass
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Jun 2, 2018
1 parent 89388db commit cc9a3b5
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 36 deletions.
55 changes: 39 additions & 16 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ Object.defineProperty(loaderContextMock, "options", {
});

implementations.forEach(implementation => {
describe(implementation.info.split("\t")[0], () => {
const implementationName = implementation.info.split("\t")[0];
describe(implementationName, () => {
syntaxStyles.forEach(ext => {
function execTest(testId, loaderOptions, webpackOptions) {
const bundleName = "bundle." + ext + "." + implementationName + ".js";
return new Promise((resolve, reject) => {
const baseConfig = merge({
entry: path.join(__dirname, ext, testId + "." + ext),
output: {
filename: "bundle." + ext + ".js"
filename: bundleName
}
}, webpackOptions);

runWebpack(baseConfig, loaderOptions, (err) => err ? reject(err) : resolve());
}).then(() => {
const actualCss = readBundle("bundle." + ext + ".js");
const actualCss = readBundle(bundleName);
const expectedCss = readCss(ext, testId);

// writing the actual css to output-dir for better debugging
Expand Down Expand Up @@ -89,12 +91,12 @@ implementations.forEach(implementation => {
});
describe("custom functions", () => {
it("should expose custom functions", () => execTest("custom-functions", {
functions: customFunctions
functions: customFunctions(implementation)
}));
});
describe("prepending data", () => {
it("should extend the data-option if present", () => execTest("prepending-data", {
data: "$prepended-data: hotpink;"
data: "$prepended-data: hotpink" + (ext == "sass" ? "\n" : ";")
}));
});
// See https://github.com/webpack-contrib/sass-loader/issues/21
Expand Down Expand Up @@ -201,8 +203,9 @@ implementations.forEach(implementation => {

sourceMap.should.not.have.property("file");
sourceMap.should.have.property("sourceRoot", fakeCwd);
// This number needs to be updated if imports.scss or any dependency of that changes
sourceMap.sources.should.have.length(10);
// This number needs to be updated if imports.scss or any dependency of that changes.
// Node Sass includes a duplicate entry, Dart Sass does not.
sourceMap.sources.should.have.length(implementation == nodeSass ? 10 : 9);
sourceMap.sources.forEach(sourcePath =>
fs.existsSync(path.resolve(sourceMap.sourceRoot, sourcePath))
);
Expand All @@ -224,8 +227,13 @@ implementations.forEach(implementation => {
runWebpack({
entry: pathToErrorFile
}, {}, (err) => {
err.message.should.match(/Property "some-value" must be followed by a ':'/);
err.message.should.match(/\(line 2, column 5\)/);
if (implementation === nodeSass) {
err.message.should.match(/Property "some-value" must be followed by a ':'/);
err.message.should.match(/\(line 2, column 5\)/);
} else {
err.message.should.match(/Expected "{"./);
err.message.should.match(/\(line 3, column 1\)/);
}
err.message.indexOf(pathToErrorFile).should.not.equal(-1);
done();
});
Expand All @@ -235,8 +243,13 @@ implementations.forEach(implementation => {
entry: pathToErrorImport
}, {}, (err) => {
// check for file excerpt
err.message.should.match(/Property "some-value" must be followed by a ':'/);
err.message.should.match(/\(line 2, column 5\)/);
if (implementation == nodeSass) {
err.message.should.match(/Property "some-value" must be followed by a ':'/);
err.message.should.match(/\(line 2, column 5\)/);
} else {
err.message.should.match(/Expected "{"./);
err.message.should.match(/\(line 3, column 1\)/);
}
err.message.indexOf(pathToErrorFile).should.not.equal(-1);
done();
});
Expand All @@ -246,8 +259,13 @@ implementations.forEach(implementation => {
entry: pathToErrorFileNotFound
}, {}, (err) => {
err.message.should.match(/@import "does-not-exist";/);
err.message.should.match(/File to import not found or unreadable: does-not-exist/);
err.message.should.match(/\(line 1, column 1\)/);
if (implementation == nodeSass) {
err.message.should.match(/File to import not found or unreadable: does-not-exist/);
err.message.should.match(/\(line 1, column 1\)/);
} else {
err.message.should.match(/Can't find stylesheet to import\./);
err.message.should.match(/\(line 1, column 9\)/);
}
err.message.indexOf(pathToErrorFileNotFound).should.not.equal(-1);
done();
});
Expand All @@ -257,8 +275,13 @@ implementations.forEach(implementation => {
entry: pathToErrorFileNotFound2
}, {}, (err) => {
err.message.should.match(/@import "\.\/another\/_module\.scss";/);
err.message.should.match(/File to import not found or unreadable: \.\/another\/_module\.scss/);
err.message.should.match(/\(line 1, column 1\)/);
if (implementation == nodeSass) {
err.message.should.match(/File to import not found or unreadable: \.\/another\/_module\.scss/);
err.message.should.match(/\(line 1, column 1\)/);
} else {
err.message.should.match(/Can't find stylesheet to import\./);
err.message.should.match(/\(line 1, column 9\)/);
}
err.message.indexOf(pathToErrorFileNotFound2).should.not.equal(-1);
done();
});
Expand Down Expand Up @@ -345,7 +368,7 @@ implementations.forEach(implementation => {
});

function readCss(ext, id) {
return fs.readFileSync(path.join(__dirname, ext, "spec", id + ".css"), "utf8").replace(CR, "");
return fs.readFileSync(path.join(__dirname, ext, "spec", implementationName, id + ".css"), "utf8").replace(CR, "");
}

function runWebpack(baseConfig, loaderOptions, done) {
Expand Down
2 changes: 1 addition & 1 deletion test/sass/import-from-npm-org-pkg.sass
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
@import ~@org/bar/foo

.foo
background: #000;
background: #000
2 changes: 1 addition & 1 deletion test/sass/imports.sass
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/* @import ~sass/some.module */
@import ~sass/some.module
/* @import url(http://example.com/something/from/the/interwebs); */
@import url(http://example.com/something/from/the/interwebs);
@import url(http://example.com/something/from/the/interwebs)
/* scoped import @import language */
.scoped-imporr
@import language
Expand Down
2 changes: 2 additions & 0 deletions test/sass/spec/dart-sass/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Will be populated by npm pretest
*.css
2 changes: 2 additions & 0 deletions test/sass/spec/node-sass/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Will be populated by npm pretest
*.css
2 changes: 2 additions & 0 deletions test/scss/spec/dart-sass/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Will be populated by npm pretest
*.css
2 changes: 2 additions & 0 deletions test/scss/spec/node-sass/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Will be populated by npm pretest
*.css
17 changes: 11 additions & 6 deletions test/tools/createSpec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"use strict";

const sass = require("node-sass");
const nodeSass = require("node-sass");
const dartSass = require("sass");
const os = require("os");
const fs = require("fs");
const path = require("path");
const customImporter = require("./customImporter.js");
const customFunctions = require("./customFunctions.js");

const implementations = [nodeSass, dartSass];
const testFolder = path.resolve(__dirname, "../");
const error = "error";

Expand Down Expand Up @@ -40,23 +42,26 @@ function createSpec(ext) {
file: url
};
},
functions: customFunctions,
includePaths: [
path.join(testFolder, ext, "another"),
path.join(testFolder, ext, "includePath")
]
};

if (/prepending-data/.test(fileName)) {
sassOptions.data = "$prepended-data: hotpink;" + os.EOL + fs.readFileSync(fileName, "utf8");
sassOptions.indentedSyntax = /\.sass$/.test(fileName);
sassOptions.data = "$prepended-data: hotpink" + (sassOptions.indentedSyntax ? "\n" : ";") +
os.EOL + fs.readFileSync(fileName, "utf8");
} else {
sassOptions.file = fileName;
}

const css = sass.renderSync(sassOptions).css;

fs.writeFileSync(path.join(basePath, "spec", fileWithoutExt + ".css"), css, "utf8");
implementations.forEach(implementation => {
sassOptions.functions = customFunctions(implementation);
const name = implementation.info.split("\t")[0];
const css = implementation.renderSync(sassOptions).css;
fs.writeFileSync(path.join(basePath, "spec", name, fileWithoutExt + ".css"), css, "utf8");
});
});
}

Expand Down
24 changes: 12 additions & 12 deletions test/tools/customFunctions.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"use strict";

const sass = require("node-sass");
module.exports = function(implementation) {
return {
"headings($from: 0, $to: 6)"(from, to) {
const f = from.getValue();
const t = to.getValue();
const list = new implementation.types.List(t - f + 1);
let i;

module.exports = {
"headings($from: 0, $to: 6)"(from, to) {
const f = from.getValue();
const t = to.getValue();
const list = new sass.types.List(t - f + 1);
let i;
for (i = f; i <= t; i++) {
list.setValue(i - f, new implementation.types.String("h" + i));
}

for (i = f; i <= t; i++) {
list.setValue(i - f, new sass.types.String("h" + i));
return list;
}

return list;
}
};
};

0 comments on commit cc9a3b5

Please sign in to comment.