|
1 | | -/* global describe, it */ |
| 1 | +/* global describe, it, after */ |
2 | 2 | /*! |
3 | 3 | * The MIT License (MIT) |
4 | 4 | * |
|
25 | 25 | // Strict mode. |
26 | 26 | 'use strict'; |
27 | 27 |
|
| 28 | +// Standard lib. |
| 29 | +const fs = require('fs'); |
| 30 | +const path = require('path'); |
| 31 | + |
28 | 32 | // Package modules. |
| 33 | +const expect = require('chai').expect; |
| 34 | +const tmp = require('tmp'); |
29 | 35 | const webpack = require('webpack'); |
30 | 36 |
|
| 37 | +// Configure. |
| 38 | +const customLoader = require.resolve('../'); |
| 39 | + |
31 | 40 | // Test suite. |
32 | 41 | describe('custom-loader', () => { |
33 | | - // Default webpack config. |
34 | | - const config = { |
35 | | - context: __dirname, |
36 | | - entry: './fixtures/entry', |
37 | | - // output: { filename: '[name].js' }, |
38 | | - // module: { |
39 | | - // rules: [ |
40 | | - // { |
41 | | - // test: /.js$/, |
42 | | - // use: 'custom-loader' |
43 | | - // } |
44 | | - // ] |
45 | | - // } |
46 | | - }; |
| 42 | + // Output. |
| 43 | + const output = tmp.fileSync({ postfix: '.js' }); |
| 44 | + after(output.removeCallback); |
47 | 45 |
|
48 | 46 | // Tests. |
49 | 47 | it('should allow a custom loader', (done) => { |
50 | | - webpack(config, (err, stats) => { |
51 | | - console.log(err, stats); |
| 48 | + const replacement = `module.exports = ${Math.random()};`; |
| 49 | + webpack({ |
| 50 | + context: __dirname, |
| 51 | + entry: './fixtures/entry.js', |
| 52 | + output: { |
| 53 | + filename: path.basename(output.name), |
| 54 | + path: path.dirname(output.name) |
| 55 | + }, |
| 56 | + module: { |
| 57 | + rules: [ |
| 58 | + { |
| 59 | + test: /.js$/, |
| 60 | + use: { |
| 61 | + loader: customLoader, |
| 62 | + options: { |
| 63 | + callback: (source) => replacement |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
| 69 | + }, (err, stats) => { |
| 70 | + if (stats.compilation.errors && stats.compilation.errors.length) { |
| 71 | + err = stats.compilation.errors[0].error; |
| 72 | + } |
| 73 | + |
| 74 | + // Check file contents. |
| 75 | + const contents = fs.readFileSync(output.name, { encoding: 'utf8' }); |
| 76 | + expect(contents).to.contain(replacement); |
| 77 | + |
| 78 | + done(err); |
| 79 | + }); |
| 80 | + }); |
| 81 | + it('should fail if a callback is not specified', (done) => { |
| 82 | + webpack({ |
| 83 | + context: __dirname, |
| 84 | + entry: './fixtures/entry.js', |
| 85 | + output: { |
| 86 | + filename: path.basename(output.name), |
| 87 | + path: path.dirname(output.name) |
| 88 | + }, |
| 89 | + module: { |
| 90 | + rules: [ |
| 91 | + { |
| 92 | + test: /.js$/, |
| 93 | + use: customLoader |
| 94 | + } |
| 95 | + ] |
| 96 | + } |
| 97 | + }, (err, stats) => { |
| 98 | + expect(stats).to.have.deep.property('compilation.errors[0].error.message'); |
| 99 | + expect(stats.compilation.errors[0].error.message).to.contain('callback'); |
52 | 100 | done(err); |
53 | 101 | }); |
54 | 102 | }); |
|
0 commit comments