saucelabs-unit-test.js 3.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*!
 * Script to run our Sauce Labs tests.
 * Copyright 2017 The Bootstrap Authors
 * Copyright 2017 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */

/*
Docs: https://wiki.saucelabs.com/display/DOCS/Platform+Configurator
Mac Opera is not currently supported by Sauce Labs
Win Opera 15+ is not currently supported by Sauce Labs
iOS Chrome is not currently supported by Sauce Labs
*/

Johann-S's avatar
Johann-S committed
15
16
17
'use strict'

const path = require('path')
18
19
const JSUnitSaucelabs = require('jsunitsaucelabs')

20
const jsUnitSaucelabs = new JSUnitSaucelabs({
21
  username: process.env.SAUCE_USERNAME,
Johann-S's avatar
Johann-S committed
22
  password: process.env.SAUCE_ACCESS_KEY,
23
  build: process.env.TRAVIS_JOB_ID
24
25
})

26
const testURL = 'http://localhost:3000/js/tests/index.html?hidepassed'
Johann-S's avatar
Johann-S committed
27
const browsersFile = require(path.resolve(__dirname, './sauce_browsers.json'))
28
29
let jobsDone = 0
let jobsSucceeded = 0
30

Johann-S's avatar
Johann-S committed
31
32
33
34
35
const waitingCallback = (error, body, id) => {
  if (error) {
    console.error(error)
    process.exit(1)
  }
36

Johann-S's avatar
Johann-S committed
37
38
39
40
41
42
43
44
  if (typeof body !== 'undefined') {
    if (!body.completed) {
      setTimeout(() => {
        jsUnitSaucelabs.getStatus(id, (error, body) => {
          waitingCallback(error, body, id)
        })
      }, 2000)
    } else {
45
46
      const test = body['js tests'][0]
      let passed = false
47
      let errorStr = false
48

Johann-S's avatar
Johann-S committed
49
      if (test.result !== null) {
50
51
52
53
54
        if (typeof test.result === 'string' && test.result === 'Test exceeded maximum duration') {
          errorStr = test.result
        } else {
          passed = test.result.total === test.result.passed
        }
Johann-S's avatar
Johann-S committed
55
      }
56

Johann-S's avatar
Johann-S committed
57
      console.log(`Tested ${testURL}`)
58
      console.log(`Platform: ${test.platform.join(', ')}`)
Johann-S's avatar
Johann-S committed
59
      console.log(`Passed: ${passed.toString()}`)
60
      console.log(`URL: ${test.url}\n`)
61
62
63
      if (errorStr) {
        console.error(errorStr)
      }
Johann-S's avatar
Johann-S committed
64
65

      if (passed) {
66
        jobsSucceeded++
67
      }
Johann-S's avatar
Johann-S committed
68
      jobsDone++
69

Johann-S's avatar
Johann-S committed
70
71
72
      // Exit
      if (jobsDone === browsersFile.length - 1) {
        jsUnitSaucelabs.stop()
73
74
75
76
77
78
79
        if (jobsDone > jobsSucceeded) {
          const failedTest = jobsDone - jobsSucceeded
          throw new Error(`Some test(s) failed (${failedTest})`)
        }

        console.log('All tests passed')
        process.exit(0)
80
81
      }
    }
Johann-S's avatar
Johann-S committed
82
83
84
85
86
  }
}

jsUnitSaucelabs.on('tunnelCreated', () => {
  browsersFile.forEach((tmpBrowser) => {
87
88
89
90
    const browsersPlatform = typeof tmpBrowser.platform === 'undefined' ? tmpBrowser.platformName : tmpBrowser.platform
    const browsersArray = [browsersPlatform, tmpBrowser.browserName, tmpBrowser.version]

    jsUnitSaucelabs.start([browsersArray], testURL, 'qunit', (error, success) => {
Johann-S's avatar
Johann-S committed
91
92
93
94
      if (typeof success !== 'undefined') {
        const taskIds = success['js tests']

        if (!taskIds || !taskIds.length) {
95
          throw new Error('Error starting tests through Sauce Labs API')
Johann-S's avatar
Johann-S committed
96
        }
97

Johann-S's avatar
Johann-S committed
98
99
100
101
102
103
104
105
        taskIds.forEach((id) => {
          jsUnitSaucelabs.getStatus(id, (error, body) => {
            waitingCallback(error, body, id)
          })
        })
      } else {
        console.error(error)
      }
106
    })
Johann-S's avatar
Johann-S committed
107
  })
108
})
109

Johann-S's avatar
Johann-S committed
110
jsUnitSaucelabs.initTunnel()