Skip to content

Commit cee4878

Browse files
committed
Remove "sinon", migrate to vi.spyOn() or vi.fn()
1 parent 3ab5810 commit cee4878

File tree

6 files changed

+67
-73
lines changed

6 files changed

+67
-73
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
"react-dom": "^18.0.0",
8787
"sass": "^1.17.0",
8888
"sass-loader": "^16.0.1",
89-
"sinon": "^14.0.0",
9089
"strip-ansi": "^6.0.0",
9190
"stylus": "^0.63.0",
9291
"stylus-loader": "^7.0.0 || ^8.1.0",

test/index.test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
'use strict';
1111

12-
import { describe, it, expect, beforeEach } from 'vitest';
13-
const sinon = require('sinon');
12+
import { describe, it, expect, beforeEach, vi } from 'vitest';
1413
const api = require('../index');
1514
const path = require('path');
1615

@@ -469,14 +468,14 @@ describe('Public API', () => {
469468
it('should call or not callbacks depending of the conditions', () => {
470469
api.configureRuntimeEnvironment('dev', {}, false);
471470

472-
const spy = sinon.spy();
471+
const spy = vi.fn();
473472
api
474473
.when((Encore) => Encore.isDev(), (Encore) => spy('is dev'))
475474
.when((Encore) => Encore.isProduction(), (Encore) => spy('is production'))
476475
.when(true, (Encore) => spy('true'));
477-
expect(spy.calledWith('is dev'), 'callback for "is dev" should be called').to.be.true;
478-
expect(spy.calledWith('is production'), 'callback for "is production" should NOT be called').to.be.false;
479-
expect(spy.calledWith('true'), 'callback for "true" should be called').to.be.true;
476+
expect(spy, 'callback for "is dev" should be called').toHaveBeenCalledWith('is dev');
477+
expect(spy, 'callback for "is production" should NOT be called').not.toHaveBeenCalledWith('is production');
478+
expect(spy, 'callback for "true" should be called').toHaveBeenCalledWith('true');
480479
});
481480
});
482481

test/loaders/less.test.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
'use strict';
1111

12-
import { describe, it, expect } from 'vitest';
12+
import { describe, it, expect, vi } from 'vitest';
1313
const WebpackConfig = require('../../lib/WebpackConfig');
1414
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
1515
const lessLoader = require('../../lib/loaders/less');
1616
const cssLoader = require('../../lib/loaders/css');
17-
const sinon = require('sinon');
1817

1918
function createConfig() {
2019
const runtimeConfig = new RuntimeConfig();
@@ -30,24 +29,24 @@ describe('loaders/less', () => {
3029
config.enableSourceMaps(true);
3130

3231
// make the cssLoader return nothing
33-
const cssLoaderStub = sinon.stub(cssLoader, 'getLoaders')
34-
.callsFake(() => []);
32+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
33+
.mockImplementation(() => []);
3534

3635
const actualLoaders = lessLoader.getLoaders(config);
3736
expect(actualLoaders).to.have.lengthOf(1);
3837
expect(actualLoaders[0].options.sourceMap).to.be.true;
39-
expect(cssLoaderStub.getCall(0).args[1]).to.be.false;
38+
expect(cssLoaderStub.mock.calls[0][1]).to.be.false;
4039

41-
cssLoader.getLoaders.restore();
40+
cssLoaderStub.mockRestore();
4241
});
4342

4443
it('getLoaders() with options callback', () => {
4544
const config = createConfig();
4645
config.enableSourceMaps(true);
4746

4847
// make the cssLoader return nothing
49-
sinon.stub(cssLoader, 'getLoaders')
50-
.callsFake(() => []);
48+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
49+
.mockImplementation(() => []);
5150

5251
config.enableLessLoader(function(lessOptions) {
5352
lessOptions.custom_option = 'foo';
@@ -60,16 +59,16 @@ describe('loaders/less', () => {
6059
custom_option: 'foo',
6160
other_option: true
6261
});
63-
cssLoader.getLoaders.restore();
62+
cssLoaderStub.mockRestore();
6463
});
6564

6665
it('getLoaders() with a callback that returns an object', () => {
6766
const config = createConfig();
6867
config.enableSourceMaps(true);
6968

7069
// make the cssLoader return nothing
71-
sinon.stub(cssLoader, 'getLoaders')
72-
.callsFake(() => []);
70+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
71+
.mockImplementation(() => []);
7372

7473
config.enableLessLoader(function(lessOptions) {
7574
lessOptions.custom_option = 'foo';
@@ -80,22 +79,22 @@ describe('loaders/less', () => {
8079

8180
const actualLoaders = lessLoader.getLoaders(config);
8281
expect(actualLoaders[0].options).to.deep.equals({ foo: true });
83-
cssLoader.getLoaders.restore();
82+
cssLoaderStub.mockRestore();
8483
});
8584

8685
it('getLoaders() with CSS modules enabled', () => {
8786
const config = createConfig();
8887
config.enableSourceMaps(true);
8988

9089
// make the cssLoader return nothing
91-
const cssLoaderStub = sinon.stub(cssLoader, 'getLoaders')
92-
.callsFake(() => []);
90+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
91+
.mockImplementation(() => []);
9392

9493
const actualLoaders = lessLoader.getLoaders(config, true);
9594
expect(actualLoaders).to.have.lengthOf(1);
9695
expect(actualLoaders[0].options.sourceMap).to.be.true;
97-
expect(cssLoaderStub.getCall(0).args[1]).to.be.true;
96+
expect(cssLoaderStub.mock.calls[0][1]).to.be.true;
9897

99-
cssLoader.getLoaders.restore();
98+
cssLoaderStub.mockRestore();
10099
});
101100
});

test/loaders/sass.test.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
'use strict';
1111

12-
import { describe, it, expect } from 'vitest';
12+
import { describe, it, expect, vi } from 'vitest';
1313
const WebpackConfig = require('../../lib/WebpackConfig');
1414
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
1515
const sassLoader = require('../../lib/loaders/sass');
1616
const cssLoader = require('../../lib/loaders/css');
17-
const sinon = require('sinon');
1817

1918
function createConfig() {
2019
const runtimeConfig = new RuntimeConfig();
@@ -30,8 +29,8 @@ describe('loaders/sass', () => {
3029
config.enableSourceMaps(true);
3130

3231
// make the cssLoader return nothing
33-
const cssLoaderStub = sinon.stub(cssLoader, 'getLoaders')
34-
.callsFake(() => []);
32+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
33+
.mockImplementation(() => []);
3534

3635
const actualLoaders = sassLoader.getLoaders(config);
3736
expect(actualLoaders).to.have.lengthOf(2);
@@ -40,18 +39,18 @@ describe('loaders/sass', () => {
4039

4140
expect(actualLoaders[1].loader).to.contain('sass-loader');
4241
expect(actualLoaders[1].options.sourceMap).to.be.true;
43-
expect(cssLoaderStub.getCall(0).args[1]).to.be.false;
42+
expect(cssLoaderStub.mock.calls[0][1]).to.be.false;
4443

45-
cssLoader.getLoaders.restore();
44+
cssLoaderStub.mockRestore();
4645
});
4746

4847
it('getLoaders() with resolve-url-loader but not sourcemaps', () => {
4948
const config = createConfig();
5049
config.enableSourceMaps(false);
5150

5251
// make the cssLoader return nothing
53-
sinon.stub(cssLoader, 'getLoaders')
54-
.callsFake(() => []);
52+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
53+
.mockImplementation(() => []);
5554

5655
const actualLoaders = sassLoader.getLoaders(config);
5756
expect(actualLoaders).to.have.lengthOf(2);
@@ -62,7 +61,7 @@ describe('loaders/sass', () => {
6261
// sourcemaps always enabled when resolve-url-loader is enabled
6362
expect(actualLoaders[1].options.sourceMap).to.be.true;
6463

65-
cssLoader.getLoaders.restore();
64+
cssLoaderStub.mockRestore();
6665
});
6766

6867
it('getLoaders() with resolve-url-loader options', () => {
@@ -74,15 +73,15 @@ describe('loaders/sass', () => {
7473
});
7574

7675
// make the cssLoader return nothing
77-
sinon.stub(cssLoader, 'getLoaders')
78-
.callsFake(() => []);
76+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
77+
.mockImplementation(() => []);
7978

8079
const actualLoaders = sassLoader.getLoaders(config);
8180
expect(actualLoaders).to.have.lengthOf(2);
8281
expect(actualLoaders[0].loader).to.contain('resolve-url-loader');
8382
expect(actualLoaders[0].options.removeCR).to.be.true;
8483

85-
cssLoader.getLoaders.restore();
84+
cssLoaderStub.mockRestore();
8685
});
8786

8887
it('getLoaders() without resolve-url-loader', () => {
@@ -93,23 +92,23 @@ describe('loaders/sass', () => {
9392
config.enableSourceMaps(false);
9493

9594
// make the cssLoader return nothing
96-
sinon.stub(cssLoader, 'getLoaders')
97-
.callsFake(() => []);
95+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
96+
.mockImplementation(() => []);
9897

9998
const actualLoaders = sassLoader.getLoaders(config);
10099
expect(actualLoaders).to.have.lengthOf(1);
101100
expect(actualLoaders[0].loader).to.contain('sass-loader');
102101
expect(actualLoaders[0].options.sourceMap).to.be.false;
103102

104-
cssLoader.getLoaders.restore();
103+
cssLoaderStub.mockRestore();
105104
});
106105

107106
it('getLoaders() with options callback', () => {
108107
const config = createConfig();
109108

110109
// make the cssLoader return nothing
111-
sinon.stub(cssLoader, 'getLoaders')
112-
.callsFake(() => []);
110+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
111+
.mockImplementation(() => []);
113112

114113
config.enableSassLoader(function(options) {
115114
options.sassOptions.custom_option = 'baz';
@@ -126,15 +125,15 @@ describe('loaders/sass', () => {
126125
other_option: true
127126
}
128127
});
129-
cssLoader.getLoaders.restore();
128+
cssLoaderStub.mockRestore();
130129
});
131130

132131
it('getLoaders() with a callback that returns an object', () => {
133132
const config = createConfig();
134133

135134
// make the cssLoader return nothing
136-
sinon.stub(cssLoader, 'getLoaders')
137-
.callsFake(() => []);
135+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
136+
.mockImplementation(() => []);
138137

139138
config.enableSassLoader(function(options) {
140139
options.custom_option = 'baz';
@@ -147,16 +146,16 @@ describe('loaders/sass', () => {
147146
const actualLoaders = sassLoader.getLoaders(config);
148147
expect(actualLoaders[1].options).to.deep.equals({ foo: true });
149148

150-
cssLoader.getLoaders.restore();
149+
cssLoaderStub.mockRestore();
151150
});
152151

153152
it('getLoaders() with CSS modules enabled', () => {
154153
const config = createConfig();
155154
config.enableSourceMaps(true);
156155

157156
// make the cssLoader return nothing
158-
const cssLoaderStub = sinon.stub(cssLoader, 'getLoaders')
159-
.callsFake(() => []);
157+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
158+
.mockImplementation(() => []);
160159

161160
const actualLoaders = sassLoader.getLoaders(config, true);
162161
expect(actualLoaders).to.have.lengthOf(2);
@@ -165,8 +164,8 @@ describe('loaders/sass', () => {
165164

166165
expect(actualLoaders[1].loader).to.contain('sass-loader');
167166
expect(actualLoaders[1].options.sourceMap).to.be.true;
168-
expect(cssLoaderStub.getCall(0).args[1]).to.be.true;
167+
expect(cssLoaderStub.mock.calls[0][1]).to.be.true;
169168

170-
cssLoader.getLoaders.restore();
169+
cssLoaderStub.mockRestore();
171170
});
172171
});

test/loaders/stylus.test.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
'use strict';
1111

12-
import { describe, it, expect } from 'vitest';
12+
import { describe, it, expect, vi } from 'vitest';
1313
const WebpackConfig = require('../../lib/WebpackConfig');
1414
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
1515
const stylusLoader = require('../../lib/loaders/stylus');
1616
const cssLoader = require('../../lib/loaders/css');
17-
const sinon = require('sinon');
1817

1918
function createConfig() {
2019
const runtimeConfig = new RuntimeConfig();
@@ -30,24 +29,24 @@ describe('loaders/stylus', () => {
3029
config.enableSourceMaps(true);
3130

3231
// make the cssLoader return nothing
33-
const cssLoaderStub = sinon.stub(cssLoader, 'getLoaders')
34-
.callsFake(() => []);
32+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
33+
.mockImplementation(() => []);
3534

3635
const actualLoaders = stylusLoader.getLoaders(config);
3736
expect(actualLoaders).to.have.lengthOf(1);
3837
expect(actualLoaders[0].options.sourceMap).to.be.true;
39-
expect(cssLoaderStub.getCall(0).args[1]).to.be.false;
38+
expect(cssLoaderStub.mock.calls[0][1]).to.be.false;
4039

41-
cssLoader.getLoaders.restore();
40+
cssLoaderStub.mockRestore();
4241
});
4342

4443
it('getLoaders() with options callback', () => {
4544
const config = createConfig();
4645
config.enableSourceMaps(true);
4746

4847
// make the cssLoader return nothing
49-
sinon.stub(cssLoader, 'getLoaders')
50-
.callsFake(() => []);
48+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
49+
.mockImplementation(() => []);
5150

5251
config.enableStylusLoader(function(stylusOptions) {
5352
stylusOptions.custom_option = 'foo';
@@ -60,16 +59,16 @@ describe('loaders/stylus', () => {
6059
custom_option: 'foo',
6160
other_option: true
6261
});
63-
cssLoader.getLoaders.restore();
62+
cssLoaderStub.mockRestore();
6463
});
6564

6665
it('getLoaders() with a callback that returns an object', () => {
6766
const config = createConfig();
6867
config.enableSourceMaps(true);
6968

7069
// make the cssLoader return nothing
71-
sinon.stub(cssLoader, 'getLoaders')
72-
.callsFake(() => []);
70+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
71+
.mockImplementation(() => []);
7372

7473
config.enableStylusLoader(function(stylusOptions) {
7574
stylusOptions.custom_option = 'foo';
@@ -80,22 +79,22 @@ describe('loaders/stylus', () => {
8079

8180
const actualLoaders = stylusLoader.getLoaders(config);
8281
expect(actualLoaders[0].options).to.deep.equals({ foo: true });
83-
cssLoader.getLoaders.restore();
82+
cssLoaderStub.mockRestore();
8483
});
8584

8685
it('getLoaders() with CSS modules enabled', () => {
8786
const config = createConfig();
8887
config.enableSourceMaps(true);
8988

9089
// make the cssLoader return nothing
91-
const cssLoaderStub = sinon.stub(cssLoader, 'getLoaders')
92-
.callsFake(() => []);
90+
const cssLoaderStub = vi.spyOn(cssLoader, 'getLoaders')
91+
.mockImplementation(() => []);
9392

9493
const actualLoaders = stylusLoader.getLoaders(config, true);
9594
expect(actualLoaders).to.have.lengthOf(1);
9695
expect(actualLoaders[0].options.sourceMap).to.be.true;
97-
expect(cssLoaderStub.getCall(0).args[1]).to.be.true;
96+
expect(cssLoaderStub.mock.calls[0][1]).to.be.true;
9897

99-
cssLoader.getLoaders.restore();
98+
cssLoaderStub.mockRestore();
10099
});
101100
});

0 commit comments

Comments
 (0)