-
-
Notifications
You must be signed in to change notification settings - Fork 771
/
module-support-assessment-test.es6
72 lines (61 loc) · 2.64 KB
/
module-support-assessment-test.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* About these tests:
* These tests concern EcmaScript Modules. That is, they test Sinon's behaviour
* when running in a EcmaScript 2015+ environment that has the Module type.
* They do not assert how code that has been transpiled into ES5 using Babel
* and similar will behave.
*/
import * as aModule from "./a-module";
// Usually one would import the default module, but one can make a form of wrapper like this
/* eslint-disable no-unused-vars */
import functionModule, * as functionModuleAlternative from "./a-function-module";
import aModuleWithDefaultExport from "./a-module-with-default";
import aModuleWithToStringTag from "./a-module-with-to-string-tag";
import referee from "@sinonjs/referee";
import sinon from "../../lib/sinon";
const { assert, refute } = referee;
function createTestSuite(action) {
var stub;
var errorRegEx = /TypeError: ES Modules cannot be (stubbed|spied)/;
describe("sinon." + action + "()", function() {
afterEach(function() {
if (stub && stub.restore) {
stub.restore();
}
});
describe("Modules with objects as their default export", function() {
it("should NOT result in error", function() {
refute.exception(function() {
stub = sinon[action](aModuleWithDefaultExport, "anExport");
});
});
it("should NOT result in error with a custom toStringTag", function() {
refute.exception(function() {
stub = sinon[action](aModuleWithToStringTag, "anExport");
});
});
it("should spy/stub an exported function", function() {
stub = sinon[action](aModuleWithDefaultExport, "anExport");
aModuleWithDefaultExport.anExport();
aModuleWithDefaultExport.anExport();
assert(stub.callCount === 2);
});
});
describe("Modules without default export", function() {
it("should give a proper error message", function() {
assert.exception(function() {
sinon[action](aModule, "anExport");
}, errorRegEx);
});
});
describe("Modules that exports a function as their default export", function() {
it("should not be possible to spy/stub the default export using a wrapper for the exports", function() {
assert.exception(function() {
stub = sinon[action](functionModuleAlternative, "default");
}, errorRegEx);
});
});
});
}
createTestSuite("stub");
createTestSuite("spy");