-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRequire.test.js
96 lines (82 loc) · 3.33 KB
/
Require.test.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const expect = require('chai').expect;
const Require = require('../lib/Require');
const requireFunction = require;
describe('Require', () => {
let sandboxRequire;
describe('#require', () => {
before(() => {
const modules = {
'./module1': () => ({}),
'./mypackage/module2': () => ({}),
};
sandboxRequire = new Require({
modules,
relativePath: '.',
globalModules: ['http'],
});
});
describe('when we pass a valid module we want to require', () => {
it('should returns the module insdie myModule is loaded', () => {
expect(sandboxRequire.require('./module1')).to.be.eql({});
});
it('should returns an avaliable global module', () => {
expect(sandboxRequire.require('http')).to.be.eql(requireFunction('http'));
});
});
describe('when we pass an invalid module', () => {
it('should throws an error if invokes a module that does not exist', () => {
const fn = () => sandboxRequire.require('does-not-exist');
expect(fn).to.throw(Error, /Cannot find module 'does-not-exist'/);
});
it('should throws an error when the module exists but is not accessible', () => {
const fn = () => sandboxRequire.require('crypto');
expect(fn).to.throw(Error, /Cannot find module 'crypto'/);
});
});
});
describe('#relativeRequire', () => {
before(() => {
const modules = {
'./module1': () => ({}),
'./mypackage2/module2': () => ({}),
'./mypackage3/module3': () => ({}),
};
sandboxRequire = new Require({
modules,
relativePath: '.',
globalModules: ['http'],
});
});
describe('when we pass a valid module we want to require', () => {
it('should be able to call module2 inside mypackage', () => {
const moduleRequire = sandboxRequire.relativeRequire('mypackage2');
expect(moduleRequire('module2')).to.be.eql({});
});
it('should be able to call module1 by full path', () => {
const moduleRequire = sandboxRequire.relativeRequire('mypackage2');
expect(moduleRequire('./module1')).to.be.eql({});
});
it('should returns an avaliable global module', () => {
const moduleRequire = sandboxRequire.relativeRequire('mypackage2');
expect(moduleRequire('http')).to.be.eql(requireFunction('http'));
});
});
describe('when we pass an invalid module', () => {
it('should throws an error if invokes a module that does not exist', () => {
const moduleRequire = sandboxRequire.relativeRequire('mypackage2');
const fn = () => moduleRequire('does-not-exist');
expect(fn).to.throw(Error, /Cannot find module 'does-not-exist'/);
});
it('should throws an error if invokes directly a module of another package', () => {
const moduleRequire = sandboxRequire.relativeRequire('mypackage2');
const fn = () => moduleRequire('module3');
expect(fn).to.throw(Error, /Cannot find module 'module3'/);
});
it('should throws an error when the module exists but is not accessible', () => {
const moduleRequire = sandboxRequire.relativeRequire('mypackage2');
const fn = () => moduleRequire('crypto');
expect(fn).to.throw(Error, /Cannot find module 'crypto'/);
});
});
});
});