Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Commit aebb006

Browse files
committed
update to latest api
1 parent f23b602 commit aebb006

File tree

3 files changed

+64
-67
lines changed

3 files changed

+64
-67
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ In your project's [plugins file](https://on.cypress.io/guides/guides/plugins.htm
1717
```javascript
1818
const webpack = require('@cypress/webpack-preprocessor')
1919

20-
module.exports = (on, config) => {
21-
on('file:preprocessor', webpack(config))
20+
module.exports = (on) => {
21+
on('file:preprocessor', webpack())
2222
}
2323
```
2424

@@ -28,15 +28,15 @@ Pass in options as the second argument to `webpack`:
2828

2929
```javascript
3030
const webpack = require('@cypress/webpack-preprocessor')
31-
module.exports = (on, config) => {
31+
module.exports = (on) => {
3232
const options = {
3333
// send in the options from your webpack.config.js, so it works the same
3434
// as your app's code
3535
webpackOptions: require('../../webpack.config'),
3636
watchOptions: {},
3737
}
3838

39-
on('file:preprocessor', webpack(config, options))
39+
on('file:preprocessor', webpack(options))
4040
}
4141
```
4242

@@ -83,14 +83,28 @@ If, for example, you want to update the options for the `babel-loader` to add th
8383
```javascript
8484
const webpack = require('@cypress/webpack-preprocessor')
8585

86-
module.exports = (on, config) => {
86+
module.exports = (on) => {
8787
const options = webpack.defaultOptions
8888
options.webpackOptions.module.rules[0].use.options.presets.push('babel-preset-stage-3')
8989

90-
on('file:preprocessor', webpack(config, options))
90+
on('file:preprocessor', webpack(options))
9191
}
9292
```
9393

94+
## Contributing
95+
96+
Run all tests once:
97+
98+
```shell
99+
npm test
100+
```
101+
102+
Run tests in watch mode:
103+
104+
```shell
105+
npm run test-watch
106+
```
107+
94108
## License
95109

96110
This project is licensed under the terms of the [MIT license](/LICENSE.md).

index.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,24 @@ const defaultOptions = {
3333
// export a function that returns another function, making it easy for users
3434
// to configure like so:
3535
//
36-
// on('file:preprocessor', webpack(config, userOptions))
36+
// on('file:preprocessor', webpack(config, options))
3737
//
38-
const preprocessor = (config, userOptions = {}) => {
39-
if (!config || typeof config.isTextTerminal !== 'boolean') {
40-
throw new Error(`Cypress Webpack Preprocessor must be called with the Cypress config as its first argument. You passed: ${JSON.stringify(config, null, 2)}`)
41-
}
42-
43-
log('user options:', userOptions)
44-
38+
const preprocessor = (options = {}) => {
39+
log('user options:', options)
4540

4641
// we return function that accepts the arguments provided by
4742
// the event 'file:preprocessor'
4843
//
4944
// this function will get called for the support file when a project is loaded
5045
// (if the support file is not disabled)
51-
// it will also get calledfor a spec file when that spec is requested by
46+
// it will also get called for a spec file when that spec is requested by
5247
// the Cypress runner
5348
//
5449
// when running in the GUI, it will likely get called multiple times
5550
// with the same filePath, as the user could re-run the tests, causing
5651
// the supported file and spec file to be requested again
57-
return (filePath, util) => {
52+
return (config) => {
53+
const filePath = config.filePath
5854
log('get', filePath)
5955

6056
// since this function can get called multiple times with the same
@@ -65,17 +61,14 @@ const preprocessor = (config, userOptions = {}) => {
6561
return bundles[filePath]
6662
}
6763

68-
// if we're in a text terminal, this is a one-time run, probably in CI
69-
// so we don't need to watch
70-
const shouldWatch = !config.isTextTerminal
71-
// util.getOutputPath returns a path alongside Cypress's other app data
72-
// files so we don't have to worry about where to put the bundled
73-
// file on disk
74-
const outputPath = util.getOutputPath(filePath)
75-
7664
// user can override the default options
77-
let webpackOptions = Object.assign({}, defaultOptions.webpackOptions, userOptions.webpackOptions)
78-
let watchOptions = Object.assign({}, defaultOptions.watchOptions, userOptions.watchOptions)
65+
let webpackOptions = Object.assign({}, defaultOptions.webpackOptions, options.webpackOptions)
66+
let watchOptions = Object.assign({}, defaultOptions.watchOptions, options.watchOptions)
67+
68+
// we're provided a default output path that lives alongside Cypress's
69+
// app data files so we don't have to worry about where to put the bundled
70+
// file on disk
71+
const outputPath = config.outputPath
7972

8073
// we need to set entry and output
8174
webpackOptions = Object.assign(webpackOptions, {
@@ -139,25 +132,25 @@ const preprocessor = (config, userOptions = {}) => {
139132
log('- compile finished for', filePath)
140133
// when the bundling is finished, we call `util.fileUpdated`
141134
// to let Cypress know to re-run the spec
142-
util.fileUpdated(filePath)
135+
config.emit('rerun')
143136
})
144137
})
145138

146-
if (shouldWatch) {
139+
if (config.shouldWatch) {
147140
log('watching')
148141
}
149142

150-
const bundler = shouldWatch ?
143+
const bundler = config.shouldWatch ?
151144
compiler.watch(watchOptions, handle) :
152145
compiler.run(handle)
153146

154147
// when the spec or project is closed, we need to clean up the cached
155148
// bundle promise and stop the watcher via `bundler.close()`
156-
util.onClose(() => {
149+
config.on('close', () => {
157150
log('close', filePath)
158151
delete bundles[filePath]
159152

160-
if (shouldWatch) {
153+
if (config.shouldWatch) {
161154
bundler.close()
162155
}
163156
})

test/index_spec.js

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,27 @@ describe('webpack preprocessor', function () {
3636
}
3737

3838
this.config = {
39-
isTextTerminal: true,
39+
filePath: 'path/to/file.js',
40+
outputPath: 'output/output.js',
41+
shouldWatch: false,
42+
on: sandbox.stub(),
43+
emit: sandbox.spy(),
4044
}
41-
this.userOptions = {}
42-
this.filePath = 'path/to/file.js'
43-
this.outputPath = 'output/output.js'
45+
this.options = {}
4446
this.util = {
4547
getOutputPath: sandbox.stub().returns(this.outputPath),
4648
fileUpdated: sandbox.spy(),
4749
onClose: sandbox.stub(),
4850
}
4951

5052
this.run = () => {
51-
return preprocessor(this.config, this.userOptions)(this.filePath, this.util)
53+
return preprocessor(this.options)(this.config)
5254
}
5355
})
5456

5557
describe('exported function', function () {
5658
it('receives user options and returns a preprocessor function', function () {
57-
expect(preprocessor(this.config, this.userOptions)).to.be.a('function')
58-
})
59-
60-
it('throws error if config is not the first argument', function () {
61-
expect(preprocessor).to.throw('must be called with the Cypress config')
59+
expect(preprocessor(this.options)).to.be.a('function')
6260
})
6361

6462
it('has defaultOptions attached to it', function () {
@@ -69,7 +67,7 @@ describe('webpack preprocessor', function () {
6967

7068
describe('preprocessor function', function () {
7169
afterEach(function () {
72-
this.util.onClose.yield() // resets the cached bundles
70+
this.config.on.withArgs('close').yield() // resets the cached bundles
7371
})
7472

7573
describe('when it finishes cleanly', function () {
@@ -87,15 +85,15 @@ describe('webpack preprocessor', function () {
8785
webpack.reset()
8886
webpack.returns(this.compilerApi)
8987

90-
const run = preprocessor(this.config, this.userOptions)
91-
run(this.filePath, this.util)
92-
run(this.filePath, this.util)
88+
const run = preprocessor(this.options)
89+
run(this.config)
90+
run(this.config)
9391
expect(webpack).to.be.calledOnce
9492
})
9593

9694
it('specifies the entry file', function () {
9795
return this.run().then(() => {
98-
expect(webpack.lastCall.args[0].entry).to.equal(this.filePath)
96+
expect(webpack.lastCall.args[0].entry).to.equal(this.config.filePath)
9997
})
10098
})
10199

@@ -108,24 +106,24 @@ describe('webpack preprocessor', function () {
108106
})
109107
})
110108

111-
it('runs when isTextTerminal is true', function () {
109+
it('runs when shouldWatch is false', function () {
112110
return this.run().then(() => {
113111
expect(this.compilerApi.run).to.be.called
114112
})
115113
})
116114

117-
it('watches when isTextTerminal is false', function () {
118-
this.config.isTextTerminal = false
115+
it('watches when shouldWatch is true', function () {
116+
this.config.shouldWatch = true
119117
this.compilerApi.watch.yields(null, this.statsApi)
120118
return this.run().then(() => {
121119
expect(this.compilerApi.watch).to.be.called
122120
})
123121
})
124122

125123
it('includes watchOptions if provided', function () {
126-
this.config.isTextTerminal = false
124+
this.config.shouldWatch = true
127125
this.compilerApi.watch.yields(null, this.statsApi)
128-
this.userOptions.watchOptions = { poll: true }
126+
this.options.watchOptions = { poll: true }
129127
return this.run().then(() => {
130128
expect(this.compilerApi.watch.lastCall.args[0]).to.eql({
131129
poll: true,
@@ -135,37 +133,29 @@ describe('webpack preprocessor', function () {
135133

136134
it('resolves with the output path', function () {
137135
return this.run().then((outputPath) => {
138-
expect(this.util.getOutputPath).to.be.calledWith(this.filePath)
139-
expect(outputPath).to.be.equal(this.outputPath)
136+
expect(outputPath).to.be.equal(this.config.outputPath)
140137
})
141138
})
142139

143-
it('calls util.fileUpdated when there is an update', function () {
140+
it('emits `rerun` when there is an update', function () {
144141
this.compilerApi.plugin.withArgs('compile').yields()
145142
return this.run().then(() => {
146-
expect(this.util.fileUpdated).to.be.calledWith(this.filePath)
147-
})
148-
})
149-
150-
it('registers onClose callback', function () {
151-
return this.run().then(() => {
152-
expect(this.util.onClose).to.be.called
153-
expect(this.util.onClose.lastCall.args[0]).to.be.a('function')
143+
expect(this.config.emit).to.be.calledWith('rerun')
154144
})
155145
})
156146

157-
it('closes bundler when isTextTerminal is false and onClose callback is called', function () {
158-
this.config.isTextTerminal = false
147+
it('closes bundler when shouldWatch is true and `close` is emitted', function () {
148+
this.config.shouldWatch = true
159149
this.compilerApi.watch.yields(null, this.statsApi)
160150
return this.run().then(() => {
161-
this.util.onClose.lastCall.args[0]()
151+
this.config.on.withArgs('close').yield()
162152
expect(this.watchApi.close).to.be.called
163153
})
164154
})
165155

166-
it('does not close bundler when isTextTerminal is true and onClose callback is called', function () {
156+
it('does not close bundler when shouldWatch is false and `close` is emitted', function () {
167157
return this.run().then(() => {
168-
this.util.onClose.lastCall.args[0]()
158+
this.config.on.withArgs('close').yield()
169159
expect(this.watchApi.close).not.to.be.called
170160
})
171161
})

0 commit comments

Comments
 (0)