-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
65 lines (53 loc) · 2.4 KB
/
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
/* eslint-disable no-useless-escape */
/*!
* rollup-plugin-prepack <https://github.com/tunnckoCore/rollup-plugin-prepack>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
/* eslint-env jest */
'use strict'
const fs = require('fs')
const path = require('path')
// const test = require('mukla')
const prepack = require('./index')
const rollup = require('rollup')
test('should main export return an object with transform() fn', () => {
const plugin = prepack()
expect(plugin.name).toEqual('prepack')
expect(typeof plugin.transform).toEqual('function')
})
test('should work as real plugin to rollup', () => {
// const expected = {
// 'abstraction-tax.js': `var some;\n(function () {\n var _$0 = this;\n\n var _0 = {\n _a: \"A\",\n _b: \"B\",\n _4: 42\n };\n _$0.some = _0;\n}).call(this);`,
// 'env-branching.js': `var fib;\n(function () {\n var _$1 = this;\n\n var _7 = function (x) {\n return x <= 1 ? x : fib(x - 1) + fib(x - 2);\n };\n\n _$1.fib = _7;\n\n var _$0 = _$1.Date.now();\n\n var _4 = 0 === _$0;\n\n var _1 = _4 ? 55 : _$0;\n\n _$1.result = _1;\n}).call(this);`,
// 'fibinacci.js': `(function () {\n var _$0 = this;\n\n _$0.x = 610;\n}).call(this);`,
// 'hello-world.js': `var hello, world, s;\n(function () {\n var _$0 = this;\n\n var _5 = function () {\n return 'hello';\n };\n\n var _6 = function () {\n return 'world';\n };\n\n _$0.hello = _5;\n _$0.world = _6;\n _$0.s = \"hello world\";\n console.log(\"hello world\");\n}).call(this);`
// }
const promiseFixtures = fs.readdirSync('./fixtures')
.map((x) => path.join(__dirname, 'fixtures', x))
.map((fp) =>
rollup
.rollup({
input: fp,
plugins: [ prepack() ]
})
.then((bundle) => bundle.generate({ format: 'cjs' }))
)
return Promise.all(promiseFixtures)
.then((results) => {
return Promise.all(results.map(({ output }) => {
const { code } = output[0]
expect(code).toMatchSnapshot()
// console.log(code)
// test.strictEqual(code.includes(expected[fileName]), true)
}))
})
})
test('should transform throws an Error if the code is not valid', () => {
function fixture () {
const plugin = prepack()
plugin.transform('foo bar baz')
}
expect(fixture).toThrow(/Syntax error: Unexpected token, expected/)
})