Skip to content

Commit

Permalink
chore: configurate browser tests (#123)
Browse files Browse the repository at this point in the history
Co-authored-by: Siarhei Bautrukevich <[email protected]>
  • Loading branch information
jeetiss and bautrukevich authored Dec 23, 2019
1 parent 2bc3f01 commit ad40735
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 53 deletions.
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ node_js:
- 'lts/*'
- '8'

env:
- NODE_ENV=production

install:
- NODE_ENV=dev npm install

env:
- NODE_ENV=production
jobs:
include:
- stage: Browser
script: npm run test:browser
46 changes: 46 additions & 0 deletions browser-test/karma.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
process.env.CHROME_BIN = require('puppeteer').executablePath()

const files = [
'../src/request/**.ts',
'../src/tools/CancelController.ts',
'../src/tools/errors.ts',
'../src/tools/getUrl.ts',
'./**/*.test.ts',
]

module.exports = function(config) {
config.set({
browserNoActivityTimeout: 60000,
browsers: ['ChromeHeadless'],
reporters: ['progress', 'karma-typescript'],
frameworks: ['jasmine', 'karma-typescript'],
captureConsole: true,

files,

plugins: ['karma-typescript', 'karma-chrome-launcher', 'karma-jasmine'],

preprocessors: files.reduce((conf, next) => {
conf[next] = ['karma-typescript']

return conf
}, {}),

karmaTypescriptConfig: {
bundlerOptions: {
addNodeGlobals: true,
constants: { 'process.env': { NODE_ENV: process.env.NODE_ENV } }
},
compilerOptions: {
target: 'es5',
lib: ['es2015', 'dom'],
strict: true,
resolveJsonModule: true,
noImplicitAny: false,
strictNullChecks: true
},
include: files,
exclude: ['../node_modules', '../test']
}
})
}
72 changes: 72 additions & 0 deletions browser-test/request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import CancelController from '../src/tools/CancelController'
import { UploadClientError } from '../src/tools/errors'
import request from '../src/request/request.browser'
import getUrl from '../src/tools/getUrl'

describe('request', () => {
it('should post', async () => {
const response = await request({
method: 'POST',
url: getUrl('https://upload.uploadcare.com', '/base/', {
jsonerrors: 1
}),
data: new Blob(Array(1000).fill(1))
})

expect(response.request.method).toBe('POST')
expect(response.status).toBe(200)
expect(response.request.url).toBe(
'https://upload.uploadcare.com/base/?jsonerrors=1'
)
expect(JSON.parse(response.data).error.content).toBe(
'UPLOADCARE_PUB_KEY is required.'
)
})

it('should get', async () => {
const response = await request({
url: getUrl('https://upload.uploadcare.com', '/from_url/status/', {
token: 'test'
})
})

expect(response.request.url).toBe(
'https://upload.uploadcare.com/from_url/status/?token=test'
)
expect(response.request.method).toBe('GET')
expect(JSON.parse(response.data).status).toBe('unknown')
})

it('should be cancellable', async () => {
const cancel = new CancelController()

setTimeout(() => {
cancel.cancel()
})

await expectAsync(
request({
url: getUrl('https://upload.uploadcare.com', '/from_url/status/', {
token: 'test'
}),
cancel
})
).toBeRejectedWithError(UploadClientError, 'Request canceled')
})

it('should handle progress', async () => {
const onProgress = jasmine.createSpy('progress')
const response = await request({
method: 'POST',
url: getUrl('https://upload.uploadcare.com', '/base/', {
jsonerrors: 1
}),
data: new Blob(Array(1000).fill(1)),
onProgress
})

expect(response.request.method).toBe('POST')
expect(response.status).toBe(200)
expect(onProgress).toHaveBeenCalled()
})
})
48 changes: 0 additions & 48 deletions karma.config.js

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"mock:start": "cd ./mock-server && npm run start",
"test": "npm run test:node",
"test:node": "jasmine-ts --project test/tsconfig.json --config=test/jasmine.config.json",
"test:browser": "karma start karma.config.js --single-run --nocache",
"test:browser": "karma start ./browser-test/karma.config.js --single-run --nocache",
"test:browser:debug": "karma start karma.config.js --browsers=Chrome --single-run=false --watch --debug --nocache",
"prebuild": "npm run clean",
"build": "tsc --build tsconfig.build.json",
Expand Down Expand Up @@ -61,7 +61,6 @@
"karma-chrome-launcher": "^3.1.0",
"karma-jasmine": "^2.0.1",
"karma-typescript": "^4.1.1",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
"prettier-config-standard": "^1.0.1",
"puppeteer": "^1.19.0",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"resolveJsonModule": true,
"moduleResolution": "node"
},
"include": ["src", "test"],
"include": ["src", "test", "browser-test"],
"exclude": ["node_modules"]
}

0 comments on commit ad40735

Please sign in to comment.