Skip to content

Commit dfad510

Browse files
authored
Merge pull request #9 from morganney/develop
test: increase coverage
2 parents 12b8ce3 + 6e9733a commit dfad510

7 files changed

+93
-59
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ You must use the wrapper around [`debug`](https://www.npmjs.com/package/debug) n
1616
Do not:
1717
* Alias your `import` or `require` of `Debug`
1818
* Spread your `require` of `Debug` over more than one line
19-
* Wrap code with `debug();like(this);debug()` on the same line
20-
* Other crazy things
2119

2220
Just make simple debug statements.
2321

@@ -55,11 +53,11 @@ localStorage.debug = 'some:feature'
5553
localStorage.debug = '*'
5654
```
5755

58-
Now reload your browser. To turn off debug logging you can `localStorage.debug = false`.
56+
Now reload your browser. To turn off debugging you can `localStorage.debug = false`.
5957

6058
### Stripping
6159

62-
To remove the logging and bundling of debug usage register this loader with webpack.
60+
To remove the logging and bundling of `debug` usage register this loader with webpack.
6361

6462
```js
6563
module: {

__tests__/integration.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import path from 'path'
2+
import { fileURLToPath } from 'url'
3+
4+
import webpack from 'webpack'
5+
import { createFsFromVolume, Volume } from 'memfs'
6+
7+
import { debugCode } from '../src/purge.js'
8+
9+
const { dirname, resolve } = path
10+
const filename = fileURLToPath(import.meta.url)
11+
const directory = dirname(filename)
12+
const build = entry => {
13+
const compiler = webpack({
14+
context: directory,
15+
entry: `./${entry}`,
16+
output: {
17+
path: resolve(directory),
18+
filename: 'bundle.js'
19+
},
20+
module: {
21+
rules: [
22+
{
23+
test: /\.js$/,
24+
exclude: /node_modules/,
25+
type: 'module',
26+
loader: resolve(directory, '../src/loader.js')
27+
}
28+
]
29+
}
30+
})
31+
32+
compiler.outputFileSystem.join = path.join.bind(path)
33+
compiler.outputFileSystem = createFsFromVolume(new Volume())
34+
35+
return new Promise((resolve, reject) => {
36+
compiler.run((err, stats) => {
37+
if (err) {
38+
return reject(err)
39+
}
40+
41+
if (stats.hasErrors()) {
42+
return reject(stats.toJson().errors)
43+
}
44+
45+
return resolve(stats)
46+
})
47+
})
48+
}
49+
50+
describe('loader', () => {
51+
it('is used by webpack to remove debug code from source files', async () => {
52+
const stats = await build('__fixtures__/file.js')
53+
const output = stats.toJson({ source: true }).modules[0].source
54+
55+
expect(output).toEqual(expect.not.stringContaining(new RegExp(debugCode, 'g')))
56+
})
57+
})

__tests__/loader.js

+29-50
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,36 @@
1-
import path from 'path'
2-
import { fileURLToPath } from 'url'
1+
import { jest } from '@jest/globals'
32

4-
import webpack from 'webpack'
5-
import { createFsFromVolume, Volume } from 'memfs'
3+
import { loader } from '../src/loader.js'
64

7-
import { debugCode } from '../src/purge.js'
8-
9-
const { dirname, resolve } = path
10-
const filename = fileURLToPath(import.meta.url)
11-
const directory = dirname(filename)
12-
const build = entry => {
13-
const compiler = webpack({
14-
context: directory,
15-
entry: `./${entry}`,
16-
output: {
17-
path: resolve(directory),
18-
filename: 'bundle.js'
19-
},
20-
module: {
21-
rules: [
22-
{
23-
test: /\.js$/,
24-
exclude: /node_modules/,
25-
type: 'module',
26-
loader: resolve(directory, '../src/loader.js')
27-
}
28-
]
29-
}
30-
})
31-
32-
compiler.outputFileSystem.join = path.join.bind(path)
33-
compiler.outputFileSystem = createFsFromVolume(new Volume())
34-
35-
return new Promise((resolve, reject) => {
36-
compiler.run((err, stats) => {
37-
if (err) {
38-
return reject(err)
39-
}
40-
41-
if (stats.hasErrors()) {
42-
return reject(stats.toJson().errors)
43-
}
44-
45-
return resolve(stats)
5+
describe('loader', () => {
6+
const getStub = () => ({
7+
callback: jest.fn((err, purgedSrc) => {
8+
return purgedSrc
469
})
4710
})
48-
}
49-
50-
describe('loader', () => {
51-
it('is used by webpack to remove debug code from source files', async () => {
52-
const stats = await build('__fixtures__/file.js')
53-
const output = stats.toJson({ source: true }).modules[0].source
5411

55-
expect(output).toEqual(expect.not.stringContaining(new RegExp(debugCode, 'g')))
12+
it('removes debug usage', () => {
13+
const stub = getStub()
14+
let src = 'import { Debug } from "webpack-strip-debug-loader"'
15+
16+
loader.call(stub, src)
17+
expect(stub.callback).toHaveBeenLastCalledWith(null, '\n', undefined, undefined)
18+
19+
src = 'debug("something")'
20+
loader.call(stub, src)
21+
expect(stub.callback).toHaveBeenLastCalledWith(null, '\n', undefined, undefined)
22+
23+
src = `if (true) {
24+
debug(String.prototype.includes('foo'))
25+
}`
26+
loader.call(stub, src)
27+
expect(stub.callback).toHaveBeenLastCalledWith(
28+
null,
29+
`if (true) {
30+
31+
}`,
32+
undefined,
33+
undefined
34+
)
5635
})
5736
})

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
* Has to wait for better ESM suport for webpack loaders
1010
* @see https://github.com/webpack/loader-runner/issues/61
1111
*/
12-
'!**/__tests__/loader.js',
12+
'!**/__tests__/integration.js',
1313
'!**/__tests__/__fixtures__/*.js'
1414
],
1515
transform: {}

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-strip-debug-loader",
3-
"version": "1.3.2",
3+
"version": "1.3.3",
44
"description": "Strips debug imports, declarations and expressions from your webpack bundles",
55
"main": "dist",
66
"type": "module",

src/loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { purge } from './purge'
1+
import { purge } from './purge.js'
22

33
const loader = function (source, map, meta) {
44
this.callback(null, purge(source), map, meta)

0 commit comments

Comments
 (0)