summaryrefslogtreecommitdiff
path: root/node_modules/get-source/test/test.browser.js
blob: ed7b5238a3736aaa2d3e0b7940cf711ded3fd82d (plain)
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
/*  TODO: make it work in Travis CI
    ------------------------------------------------------------------------ */

const selenium = require ('selenium-webdriver/testing')

/*  ------------------------------------------------------------------------ */

selenium.describe ('Chrome test', (done) => {

    const webdriver = require ('selenium-webdriver')
        , path      = require ('path')
        , fs        = require ('fs')
        , memFS     = new (require ('memory-fs')) ()
        , it        = selenium.it
        , webpack   = require ('webpack')
        , logging   = require ('selenium-webdriver/lib/logging')

    let driver

/*  Prepare ChromeDriver (with CORS disabled and log interception enabled)   */

    selenium.before (() => driver =
                            new webdriver
                                    .Builder ()
                                    .withCapabilities (
                                        webdriver.Capabilities
                                                .chrome ()
                                                .setLoggingPrefs (new logging.Preferences ().setLevel (logging.Type.BROWSER, logging.Level.ALL))
                                                .set ('chromeOptions', {
                                                        'args': ['--disable-web-security'] }))
                                    .build ())

    selenium.after  (() => driver.quit ())

    it ('works', async () => {

    /*  Compile get-source   */

        const compiledScript = await (new Promise (resolve => { Object.assign (webpack ({ 

                entry: './test/files/get-source.webpack.entry.js',
                output: { path: '/', filename: 'get-source.webpack.compiled.js' },
                plugins: [ new webpack.IgnorePlugin(/^fs$/) ]

            }), { outputFileSystem: memFS }).run ((err, stats) => {

                if (err) throw err

                resolve (memFS.readFileSync ('/get-source.webpack.compiled.js').toString ('utf-8'))
            })
        }))

    /*  Inject it into Chrome   */

        driver.get ('file://' + path.resolve ('./test/files/test.html'))
        driver.executeScript (compiledScript)

    /*  Execute test    */

        const exec = fn => driver.executeScript (`(${fn.toString ()})()`)

        try {

            await exec (function () {

                path.relativeToFile ('http://foo.com/scripts/bar.js', '../bar.js.map')
                    .should.equal ('http://foo.com/bar.js.map')

                path.relativeToFile ('http://foo.com/scripts/bar.js', 'http://bar.js.map')
                    .should.equal ('http://bar.js.map')

                path.relativeToFile ('http://foo.com/scripts/bar.js', '/bar.js.map')
                    .should.equal ('file:///bar.js.map')

                path.relativeToFile ('http://foo.com/scripts/bar.js', '//bar.com/bar.js.map')
                    .should.equal ('http://bar.com/bar.js.map')

                var loc = getSource ('../original.uglified.beautified.js').resolve ({ line: 2, column: 4 })

                loc.line.should.equal (4)
                loc.column.should.equal (2)
                loc.sourceFile.path.should.contain ('test/files/original.js')
                loc.sourceLine.should.equal ('\treturn \'hello world\' }')
            })

        } catch (e) { throw e } finally {

            driver.manage ().logs ().get (logging.Type.BROWSER).then (entries => {
                entries.forEach (entry => {
                    console.log('[BROWSER] [%s] %s', entry.level.name, entry.message);
                })
            })
        }
    })
})

/*  ------------------------------------------------------------------------ */