Issues (5)

test/test.node.js (1 issue)

Severity
1
"use strict";
2
3
/*  NOTE: I've used supervisor to auto-restart mocha, because mocha --watch
4
          didn't work for selenium tests (not reloading them)...
5
          ------------------------------------------------------------------ */
6
                    
7
require ('chai').should ()
8
9
/*  ------------------------------------------------------------------------ */
10
11
describe ('get-source', () => {
0 ignored issues
show
Parsing error: Unexpected token =
Loading history...
12
13
    const getSource = require ('../get-source'),
14
          fs        = require ('fs'),
15
          path      = require ('path')
16
17
    it ('cache sanity check', () => {
18
19
        getSource ('./test.js').should.equal     (getSource ('./test.js'))
20
        getSource ('./test.js').should.not.equal (getSource ('./package.json'))
21
    })
22
23
    it ('reads sources (not sourcemapped)', () => {
24
25
        const original = getSource ('./test/files/original.js')
26
27
        original.path.should.equal (path.resolve ('./test/files/original.js')) // resolves input paths
28
        original.text.should.equal (fs.readFileSync ('./test/files/original.js', { encoding: 'utf-8' }))
29
        original.lines.should.deep.equal ([
30
            '/*\tDummy javascript file\t*/',
31
            '',
32
            'function hello () {',
33
            '\treturn \'hello world\' }'
34
        ])
35
36
        const resolved = original.resolve ({ line: 4, column: 1 })
37
38
        resolved.line.should.equal (4)
39
        resolved.column.should.equal (1)
40
        resolved.sourceFile.should.equal (original)
41
        resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
42
    })
43
44
    it ('reads sources (sourcemapped, with external links)', () => {
45
46
        const uglified = getSource ('./test/files/original.uglified.js')
47
48
        uglified.path.should.equal (path.resolve ('./test/files/original.uglified.js'))
49
        uglified.lines.should.deep.equal ([
50
            'function hello(){return"hello world"}',
51
            '//# sourceMappingURL=original.uglified.js.map',
52
            ''
53
        ])
54
55
        uglified.sourceMap.should.not.equal (undefined)
56
        uglified.sourceMap.should.equal (uglified.sourceMap) // memoization should work
57
58
        const resolved = uglified.resolve ({ line: 1, column: 18 }) // should be tolerant to column omission
59
60
        resolved.line.should.equal (4)
61
        resolved.column.should.equal (2)
62
        resolved.sourceFile.should.equal (getSource ('./test/files/original.js'))
63
        resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
64
    })
65
66
    it ('reads sources (sourcemapped, with embedded sources)', () => {
67
68
        const uglified = getSource ('./test/files/original.uglified.with.sources.js')
69
70
        uglified.path.should.equal (path.resolve ('./test/files/original.uglified.with.sources.js'))
71
        uglified.lines.should.deep.equal ([
72
            'function hello(){return"hello world"}',
73
            '//# sourceMappingURL=original.uglified.with.sources.js.map',
74
            ''
75
        ])
76
77
        uglified.sourceMap.should.not.equal (undefined)
78
79
        const resolved = uglified.resolve ({ line: 1, column: 18 })
80
81
        resolved.line.should.equal (4)
82
        resolved.column.should.equal (2)
83
        resolved.sourceFile.path.should.equal (path.resolve ('./test/files') + '/## embedded ##') // I've changed the filename manually, by editing .map file
84
        resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
85
    })
86
87
    it ('reads sources (sourcemapped, with inline base64 sourcemaps)', () => {
88
89
        const babeled = getSource ('./test/files/original.babeled.with.inline.sourcemap.js')
90
91
        babeled.sourceMap.should.not.equal (undefined)
92
        babeled.sourceMap.file.path.should.equal (babeled.path)
93
94
        const resolved = babeled.resolve ({ line: 6, column: 1 })
95
96
        resolved.line.should.equal (4)
97
        resolved.column.should.equal (2)
98
        resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
99
    })
100
101
    it ('supports even CHAINED sourcemaps!', () => {
102
103
    /*  original.js → original.uglified.js → original.uglified.beautified.js    */
104
105
        const beautified = getSource ('./test/files/original.uglified.beautified.js')
106
107
        beautified.path.should.equal (path.resolve ('./test/files/original.uglified.beautified.js'))
108
        beautified.text.should.equal (fs.readFileSync ('./test/files/original.uglified.beautified.js', { encoding: 'utf-8' }))
109
110
        beautified.sourceMap.should.not.equal (undefined)
111
112
        const resolved = beautified.resolve ({ line: 2, column: 4 })
113
114
        resolved.line.should.equal (4)
115
        resolved.column.should.equal (2)
116
        resolved.sourceFile.path.should.equal (path.resolve ('./test/files/original.js'))
117
        resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
118
    })
119
120
    it ('does some error handling', () => {
121
122
        const nonsense = getSource ('abyrvalg')
123
124
        nonsense.text.should.equal ('')
125
        nonsense.error.should.be.an.instanceof (Error)
126
127
        const resolved = nonsense.resolve ({ line: 5, column: 0 })
128
129
        resolved.error.should.equal (nonsense.error)
130
        resolved.sourceLine.should.equal ('')
131
    })
132
133
    it ('allows absolute paths', () => {
134
135
        getSource (require ('path').resolve ('./test.js')).should.equal (getSource ('./test.js'))
136
    })
137
138
    it ('caching works', () => {
139
        
140
        const files =
141
                [   './test.js',
142
                    './package.json',
143
                    './test/files/original.js',
144
                    './test/files/original.uglified.js',
145
                    './test/files/original.uglified.js.map',
146
                    './test/files/original.uglified.with.sources.js',
147
                    './test/files/original.uglified.with.sources.js.map',
148
                    './test/files/original.babeled.with.inline.sourcemap.js',
149
                    './test/files/original.uglified.beautified.js',
150
                    './test/files/original.uglified.beautified.js.map',
151
                    './abyrvalg' ]
152
            
153
        Object.keys (getSource.getCache ()).should.deep.equal (files.map (x => path.resolve (x)))
154
155
        getSource.resetCache ()
156
157
        Object.keys (getSource.getCache ()).length.should.equal (0)
158
    })
159
})