Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyMohnatkin committed Sep 4, 2017
1 parent c19ac91 commit 23fa5c6
Show file tree
Hide file tree
Showing 12 changed files with 3,992 additions and 12 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -1,3 +1,4 @@
node_modules/
lib/
example/
test/output
3 changes: 3 additions & 0 deletions .eslintrc.yml
Expand Up @@ -4,5 +4,8 @@ extends: eslint-config-shakacode/base
globals:
__DEBUG__: true

env:
jest: true

ecmaFeatures:
restParams: true
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@ node_modules/
*.log
npm-debug.log*
.DS_Store
test/output
.vscode
9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -14,7 +14,8 @@
"prepublish": "npm run prerelease",
"release:patch": "scripts/release patch",
"release:minor": "scripts/release minor",
"release:major": "scripts/release major"
"release:major": "scripts/release major",
"test": "jest"
},
"authors": [
"Justin Gordon <justin.gordon@gmail.com> (https://github.com/justin808)",
Expand Down Expand Up @@ -45,6 +46,10 @@
"babel-preset-es2015": "^6.1.18",
"eslint": "^1.10.1",
"eslint-config-airbnb": "^1.0.0",
"eslint-config-shakacode": "0.0.1"
"eslint-config-shakacode": "0.0.1",
"jest": "^20.0.4",
"raw-loader": "^0.5.1",
"webpack": "2.2.0",
"webpack-merge": "^4.1.0"
}
}
20 changes: 10 additions & 10 deletions src/utils/rewriteImports.js
Expand Up @@ -4,6 +4,14 @@ import logger from './logger';

const importRegexp = /@import\s+(?:'([^']+)'|"([^"]+)"|([^\s;]+))/g;

export const getNewImportPath = (oldImportPath, absoluteImportPath, moduleContext) => {
// from node_modules
if ((/^\~/).test(oldImportPath)) {
return oldImportPath;
}
return path.relative(moduleContext, absoluteImportPath);
};

export default (error, file, contents, moduleContext, callback) => {
if (error) {
logger.debug('Resources: **not found**');
Expand All @@ -14,20 +22,12 @@ export default (error, file, contents, moduleContext, callback) => {
return callback(null, contents);
}

const getNewImportPath = (oldImportPath) => {
const absoluteImportPath = path.join(path.dirname(file), oldImportPath);

// from node_modules
if ((/^\~/).test(oldImportPath)) {
return oldImportPath;
}
return path.relative(moduleContext, absoluteImportPath);
};

const rewritten = contents.replace(importRegexp, (entire, single, double, unquoted) => {
const oldImportPath = single || double || unquoted;

const newImportPath = getNewImportPath(oldImportPath);
const absoluteImportPath = path.join(path.dirname(file), oldImportPath);
const newImportPath = getNewImportPath(oldImportPath, absoluteImportPath, moduleContext);
logger.debug(`Resources: @import of ${oldImportPath} changed to ${newImportPath}`);

const lastCharacter = entire[entire.length - 1];
Expand Down
30 changes: 30 additions & 0 deletions test/__snapshots__/index.test.js.snap
@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`sass-resources-loader resources should include resources 1`] = `
"$text-color: $ccc;
div {
display: block;
color: $text-color;
}"
`;

exports[`sass-resources-loader resources should parse array resources 1`] = `
"
div {
display: block;
color: $text-color;
}
"
`;

exports[`sass-resources-loader resources should parse resource 1`] = `
"
div {
display: block;
color: $text-color;
}
"
`;
126 changes: 126 additions & 0 deletions test/index.test.js
@@ -0,0 +1,126 @@
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const pathToLoader = require.resolve('../lib/loader.js');

function runWebpack(baseConfig, done) {
const webpackConfig = merge({
output: {
path: path.join(__dirname, 'output'),
libraryTarget: 'commonjs2',
},
}, baseConfig);

webpack(webpackConfig, (webpackErr, stats) => {
const err = webpackErr ||
(stats.hasErrors() && stats.compilation.errors[0]) ||
(stats.hasWarnings() && stats.compilation.warnings[0]);

done(err || null);
});
}

function execTest(testId, options) {
return new Promise((resolve, reject) => {
const baseConfig = merge({
entry: path.resolve(__dirname, 'scss', `${testId}.scss`),
output: {
filename: `${testId}.js`,
},
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: 'raw-loader' },
{
loader: pathToLoader,
options,
},
],
}],
},
});

runWebpack(baseConfig, (err) => err ? reject(err) : resolve());
});
}

describe(`sass-resources-loader`, () => {
describe('resources', () => {
it('should parse resource', () => execTest('empty', {
resources: path.resolve(__dirname, './scss/*.scss'),
}).then(() => {
const output = require('./output/empty');
expect(output).toMatchSnapshot();
}));

it('should parse array resources', () => execTest('empty2', {
resources: [
path.resolve(__dirname, './scss/*.scss'),
],
}).then(() => {
const output = require('./output/empty2');
expect(output).toMatchSnapshot();
}));

it('should include resources', () => execTest('imports', {
resources: [
path.resolve(__dirname, './scss/variables/*.scss'),
],
}).then(() => {
const output = require('./output/imports');
expect(output).toMatchSnapshot();
}));

it('should throw error when no resources provided', (done) => {
runWebpack({
entry: path.resolve(__dirname, 'scss', 'empty.scss'),
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: 'raw-loader' },
{
loader: pathToLoader,
},
],
}],
},
}, (err) => {
expect(err.message).toMatch(/Can\'t find sass resources in your config/);
expect(err.message).toMatch(/Make sure loader.options.resources exists/);
done();
});
});
it('should throw error when resources are empty', (done) => {
runWebpack({
entry: path.resolve(__dirname, 'scss', 'empty.scss'),
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: 'raw-loader' },
{
loader: pathToLoader,
options: {
resources: [],
},
},
],
}],
},
}, (err) => {
expect(err.message).toMatch(/Something wrong with provided resources/);
expect(err.message).toMatch(/Make sure \'options.resources\' is String or Array of Strings/);
done();
});
});
});

describe('imports', () => {
it('should not rewrite path for imports with ~', () => {
const getNewImportPath = require('../lib/utils/rewriteImports').getNewImportPath;
expect(getNewImportPath('~/bootstrap', '', '')).toMatch('~/bootstrap');
});
});
});
Empty file added test/scss/empty.scss
Empty file.
Empty file added test/scss/empty2.scss
Empty file.
4 changes: 4 additions & 0 deletions test/scss/imports.scss
@@ -0,0 +1,4 @@
div {
display: block;
color: $text-color;
}
1 change: 1 addition & 0 deletions test/scss/variables/variables.scss
@@ -0,0 +1 @@
$text-color: $ccc;

0 comments on commit 23fa5c6

Please sign in to comment.