|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var should = require('should'); |
| 4 | +var path = require('path'); |
| 5 | +var webpack = require('webpack'); |
| 6 | +var fs = require('fs'); |
| 7 | + |
| 8 | +var CR = /\r/g; |
| 9 | + |
| 10 | +function readCss(ext, id) { |
| 11 | + return fs.readFileSync(path.join(__dirname, ext, id + '.css'), 'utf8').replace(CR, ''); |
| 12 | +} |
| 13 | + |
| 14 | +function test(name, id, query) { |
| 15 | + it(name, function (done) { |
| 16 | + var exts = ['scss', 'sass']; |
| 17 | + var pending = exts.length; |
| 18 | + |
| 19 | + query = query || ''; |
| 20 | + |
| 21 | + exts.forEach(function (ext) { |
| 22 | + var expectedCss = readCss(ext, id); |
| 23 | + var sassFile = 'raw!' + |
| 24 | + path.resolve(__dirname, '../index.js') + '?' + |
| 25 | + query + |
| 26 | + (ext === 'sass'? '&indentedSyntax=sass' : '') + '!' + |
| 27 | + path.join(__dirname, ext, id + '.' + ext); |
| 28 | + var actualCss; |
| 29 | + |
| 30 | + // run asynchronously |
| 31 | + webpack({ |
| 32 | + entry: sassFile, |
| 33 | + output: { |
| 34 | + path: __dirname + '/output', |
| 35 | + filename: 'bundle.' + ext + '.js', |
| 36 | + libraryTarget: 'commonjs2' |
| 37 | + } |
| 38 | + }, function onCompilationFinished(err, stats) { |
| 39 | + if (err) { |
| 40 | + return done(err); |
| 41 | + } |
| 42 | + if (stats.hasErrors()) { |
| 43 | + return done(stats.compilation.errors[0]); |
| 44 | + } |
| 45 | + if (stats.hasWarnings()) { |
| 46 | + return done(stats.compilation.warnings[0]); |
| 47 | + } |
| 48 | + delete require.cache[path.resolve(__dirname, './output/bundle.' + ext + '.js')]; |
| 49 | + |
| 50 | + actualCss = require('./output/bundle.' + ext + '.js'); |
| 51 | + // writing the actual css to output-dir for better debugging |
| 52 | + //fs.writeFileSync(__dirname + '/output/' + name + '.' + ext + '.async.css', actualCss, 'utf8'); |
| 53 | + actualCss.should.eql(expectedCss); |
| 54 | + |
| 55 | + pending--; |
| 56 | + if (pending === 0) { |
| 57 | + done(); |
| 58 | + } |
| 59 | + }); |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +describe('sass-loader', function () { |
| 65 | + test('should compile simple sass without errors', 'language'); |
| 66 | + test('should resolve imports correctly', 'imports'); |
| 67 | + test('should pass the include paths to node-sass', 'include-paths', |
| 68 | + 'includePaths[]=' + path.resolve(__dirname, './sass/another') + '&' + |
| 69 | + 'includePaths[]=' + path.resolve(__dirname, './scss/another')); |
| 70 | +}); |
0 commit comments