Completed
Push — master ( 8c53f9...1d869a )
by Marcelo
33s
created

test/module.spec.js   A

Complexity

Total Complexity 9
Complexity/F 1

Size

Lines of Code 71
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 1 Features 0
Metric Value
cc 0
wmc 9
c 10
b 1
f 0
nc 1
mnd 0
bc 8
fnc 9
dl 0
loc 71
rs 10
bpm 0.8888
cpm 1
noi 1
1
import { expect } from 'chai';
2
import {
3
    compileModulesFromSource,
4
    evaluateModules,
5
    inspect
6
} from '../src/module';
7
import { createVM } from '../src/vm';
8
9
describe('module.js', () => {
10
    describe('Module compilation', () => {
11
        it('should compile and evaluate modules', () => {
12
            const modules = [
13
                ['drag-queen.json', JSON.stringify({
14
                    alaska: 1,
15
                    courtney: 2
16
                })],
17
                ['workaround.js', `
18
                    module.exports = 42;
19
                `]
20
            ];
21
22
            const vm = createVM({});
23
            const result = evaluateModules(vm, modules);
24
            expect(result).to.be.an('object');
25
            expect(result).to.have.property('drag-queen.json');
26
            expect(result).to.have.property('workaround.js');
27
            expect(result).property('workaround.js').to.equals(42);
28
            expect(result).property('drag-queen.json').to.have.property('alaska');
29
        });
30
31
        it('should get module files from AST', () => {
32
            const { code, modules } = inspect(`
33
                import jquery from 'jquery';
34
                import isThirteen from 'is-thirteen';
35
                import workarounds from './gambiarras';
36
            `);
37
38
            expect(code).to.be.a('string');
39
            expect(modules).to.be.an('array');
40
            expect(modules).to.have.lengthOf(3);
41
            expect(modules).to.contain('jquery');
42
            expect(modules).to.contain('is-thirteen');
43
            expect(modules).to.contain('./gambiarras');
44
        });
45
46
        it('should reject when required file has no loader', () => {
47
            const source = 'import config from "./_config.yml"';
48
            return compileModulesFromSource(source)
49
                .then((x) => {
50
                    throw new Error('Should never fall here');
51
                })
52
                .catch(({ message }) => {
53
                    expect(message).to.match(/Unknown module loader for file/);
54
                });
55
        });
56
57
        it('should reject compilation of unknown file', () => {
58
            const modules = [
59
                ['Main.hs', `
60
                    module Main where
61
62
                    main :: IO ()
63
                    main = putStrLn "Hello from Haskell!"
64
                `]
65
            ];
66
            const vm = createVM({});
67
            expect(() => evaluateModules(vm, modules))
68
                .to.throw('Unknown file type for Main.hs');
69
        });
70
    });
71
});