-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support and tests for webpack (#45)
- Loading branch information
1 parent
1b47153
commit 29d5d85
Showing
14 changed files
with
411 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ coverage | |
package-lock.json | ||
yarn.lock | ||
.vscode | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2019, Google, LLC. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import * as execa from 'execa'; | ||
import * as express from 'express'; | ||
import * as http from 'http'; | ||
|
||
const port = 7172; | ||
|
||
async function listen( | ||
app: express.Express, port: number): Promise<http.Server> { | ||
return new Promise((resolve, reject) => { | ||
const server = app.listen(port, (err: Error) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(server); | ||
}); | ||
}); | ||
} | ||
|
||
// Starts a web server that browser tests will use, then runs actual browser | ||
// tests. | ||
async function main() { | ||
const app = express(); | ||
app.get('/path', (req: express.Request, res: express.Response) => { | ||
if (req.header('origin')) { | ||
res.set('Access-Control-Allow-Origin', req.header('origin')); | ||
} | ||
res.send('response'); | ||
}); | ||
app.get('/querystring', (req: express.Request, res: express.Response) => { | ||
if (req.header('origin')) { | ||
res.set('Access-Control-Allow-Origin', req.header('origin')); | ||
} | ||
const query = req.query.query; | ||
res.send(query || ''); | ||
}); | ||
|
||
const server = await listen(app, port); | ||
console.log(`[http server] I'm listening on port ${port}! Starting karma.`); | ||
const result = await execa('karma', ['start'], {stdio: 'inherit'}); | ||
server.close(); | ||
console.log( | ||
`[http server] Karma has finished! I'm no longer listening on port ${ | ||
port}!`); | ||
process.exit(result.failed ? 1 : 0); | ||
} | ||
|
||
main().catch(err => { | ||
console.log('Error:', err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as assert from 'assert'; | ||
|
||
import {request} from '../src/index'; | ||
const port = 7172; // should match the port defined in `webserver.ts` | ||
|
||
describe('💻 browser tests', () => { | ||
it('should just work from browser', async () => { | ||
const result = await request({url: `http://localhost:${port}/path`}); | ||
assert.strictEqual(result.status, 200); | ||
assert.strictEqual(result.data, 'response'); | ||
}); | ||
|
||
it('should pass querystring parameters from browser', async () => { | ||
const result = await request({ | ||
url: `http://localhost:${port}/querystring`, | ||
params: {query: 'value'} | ||
}); | ||
assert.strictEqual(result.status, 200); | ||
assert.strictEqual(result.data, 'value'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright 2019 Google LLC. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Karma configuration | ||
// Use `npm run browser-test` to run browser tests with Karma. | ||
const fs = require('fs'); | ||
const isDocker = require('is-docker')(); | ||
|
||
const webpackConfig = require('./webpack-tests.config.js'); | ||
process.env.CHROME_BIN = fs.existsSync('/usr/bin/chromium-browser') | ||
? '/usr/bin/chromium-browser' | ||
: require('puppeteer').executablePath(); | ||
|
||
module.exports = function (config) { | ||
config.set({ | ||
// base path that will be used to resolve all patterns (eg. files, exclude) | ||
basePath: '', | ||
|
||
// frameworks to use | ||
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter | ||
frameworks: ['mocha'], | ||
|
||
// list of files / patterns to load in the browser | ||
files: ['./browser-test/test.*.ts'], | ||
|
||
// list of files / patterns to exclude | ||
exclude: [], | ||
|
||
// preprocess matching files before serving them to the browser | ||
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor | ||
preprocessors: { | ||
'./src/*.ts': ['coverage'], | ||
'./src/**/*.ts': ['coverage'], | ||
'./browser-test/*.ts': ['webpack', 'sourcemap'] | ||
}, | ||
|
||
webpack: webpackConfig, | ||
|
||
// test results reporter to use | ||
// possible values: 'dots', 'progress' | ||
// available reporters: https://npmjs.org/browse/keyword/karma-reporter | ||
reporters: ['progress', 'coverage', 'remap-coverage'], | ||
|
||
coverageReporter: { type: 'in-memory' }, | ||
remapCoverageReporter: { html: './coverage' }, | ||
|
||
// web server port | ||
port: 9876, | ||
|
||
// enable / disable colors in the output (reporters and logs) | ||
colors: true, | ||
|
||
// level of logging | ||
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG | ||
logLevel: config.LOG_INFO, | ||
|
||
// enable / disable watching file and executing tests whenever any file changes | ||
autoWatch: false, | ||
|
||
// start these browsers | ||
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | ||
browsers: ['ChromeCustom'], | ||
customLaunchers: { | ||
ChromeCustom: { | ||
base: 'ChromeHeadless', | ||
// We must disable the Chrome sandbox when running Chrome inside Docker (Chrome's sandbox needs | ||
// more permissions than Docker allows by default) | ||
flags: isDocker ? ['--no-sandbox'] : [] | ||
} | ||
}, | ||
|
||
// Continuous Integration mode | ||
// if true, Karma captures browsers, runs the tests and exits | ||
singleRun: true, | ||
|
||
// Concurrency level | ||
// how many browser should be started simultaneous | ||
concurrency: Infinity, | ||
|
||
// set correct MIME type when serving .ts files (already compiled to JavaScript): | ||
mime: { | ||
'text/javascript': ['ts'] | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright 2019 Google LLC. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export function isBrowser(): boolean { | ||
return typeof (window) !== 'undefined'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright 2019 Google LLC. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Use `npm run webpack` to produce Webpack bundle for this library. | ||
|
||
const path = require('path'); | ||
|
||
module.exports = { | ||
entry: './src/index.ts', | ||
resolve: { | ||
extensions: ['.ts', '.js', '.json'], | ||
alias: { | ||
'../../package.json': path.resolve(__dirname, 'package.json') | ||
} | ||
}, | ||
output: { | ||
filename: 'bundle.min.js', | ||
path: path.resolve(__dirname, 'dist') | ||
}, | ||
node: { | ||
child_process: 'empty', | ||
fs: 'empty', | ||
crypto: 'empty' | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /node_modules\/https-proxy-agent\//, | ||
use: 'null-loader' | ||
}, | ||
{ | ||
test: /\.ts$/, | ||
use: 'ts-loader', | ||
exclude: /node_modules/ | ||
} | ||
] | ||
}, | ||
mode: 'production', | ||
plugins: [] | ||
}; |
Oops, something went wrong.