-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
114 lines (99 loc) · 3.6 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const fs = require('fs')
const path = require('path')
const expect = require('chai').expect
const rollup = require('rollup')
const commonjs = require('rollup-plugin-commonjs')
const shim = require('../lib')
const fixture = filePath => path.join(__dirname, `fixtures/${filePath}`)
const saveActual = (filePath, content) => {
const folderPath = path.dirname(filePath)
if (fs.existsSync(folderPath)) {
fs.writeFileSync(filePath, content)
}
}
describe('index.js', () => {
describe('#shim', () => {
it('should return a rollup plugin', () => {
const plugin = shim()
expect(plugin).to.have.property('resolveId').a('function')
expect(plugin).to.have.property('load').a('function')
})
})
describe('#shim.resolveId', () => {
it('should ignore unspecified modules', () => {
const plugin = shim({foo: 'hello'})
expect(plugin.resolveId('missing')).to.equal(null)
})
it('should create an ID for specified modules', () => {
const plugin = shim({foo: 'hello'})
expect(plugin.resolveId('foo')).to.match(/rollup-plugin-shim-\d+/)
})
it('should create multiple ID for specified modules', () => {
const plugin = shim({foo: 'hello', bar: 'other'})
const idA = plugin.resolveId('foo')
const idB = plugin.resolveId('bar')
expect(idA).to.not.equal(idB)
expect(idA).to.match(/rollup-plugin-shim-\d+/)
expect(idB).to.match(/rollup-plugin-shim-\d+/)
})
})
describe('#shim.load', () => {
it('should ignore unspecified modules', () => {
const plugin = shim({foo: 'hello'})
const id = plugin.resolveId('missing')
const value = plugin.load(id)
expect(value).to.equal(null)
})
it('should return string content for specified module', () => {
const plugin = shim({foo: 'hello'})
const id = plugin.resolveId('foo')
const value = plugin.load(id)
expect(value).to.equal('hello')
})
})
describe('rollup', () => {
const foo = 'export const named = 10'
const bar = 'export default function (a, b, c) { return a + b.itemA + b.itemB + c() }'
const baz = 'export const itemA = 1\nexport const itemB = 2'
const depended = 'export function depended () { return 5 }'
it('should work with rollup', () => {
const options = {
entry: fixture('es6.js'),
plugins: [
shim({foo, bar, baz, [fixture('es6-dep.js')]: depended}),
],
}
return rollup.rollup(options)
.then(bundle => bundle.generate({format: 'cjs'}))
.then(generated => {
const code = generated.code
saveActual(fixture('out/es6.js'), code)
expect(code).to.contain('named = 10')
expect(code).to.contain('itemA = 1')
expect(code).to.contain('itemB = 2')
expect(code).to.contain('return 5') // depended swapped
expect(code).to.not.contain('return 100') // depended original
const result = eval(code) // eslint-disable-line no-eval
expect(result).to.eql(18)
})
})
it('should work with rollup and commonjs', () => {
const options = {
entry: fixture('node.js'),
onwarn: () => undefined,
plugins: [
shim({foo, bar, baz, [fixture('node-dep.js')]: depended}),
commonjs(),
],
}
return rollup.rollup(options)
.then(bundle => bundle.generate({format: 'cjs'}))
.then(generated => {
const code = generated.code
saveActual(fixture('out/node.js'), code)
const result = eval(code) // eslint-disable-line no-eval
expect(result).to.eql(18)
})
})
})
})