Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions packages/browser-test-runner/src/createKarmaConfig.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
import fs from 'fs'
import type { Configuration, ExternalItem } from 'webpack'

const DEBUG_MODE = process.env.BROWSER_TEST_DEBUG_MODE ?? false

export const createKarmaConfig = (
testPaths: string[], webpackConfig: () => Record<string, any>, localDirectory: string
testPaths: string[], webpackConfig: () => Configuration, localDirectory?: string
): (config: any) => any => {
const setupFiles = [__dirname + '/karma-setup.js']
const localSetupFile = localDirectory + '/karma-setup.js'
if (fs.existsSync(localSetupFile)) {
setupFiles.push(localSetupFile)

if (localDirectory !== undefined) {
const localSetupFile = localDirectory + '/karma-setup.js'
if (fs.existsSync(localSetupFile)) {
setupFiles.push(localSetupFile)
}
}

const preprocessors: Record<string, string[]> = {}
setupFiles.forEach((f) => preprocessors[f] = ['webpack'])
testPaths.forEach((f) => preprocessors[f] = ['webpack', 'sourcemap'])
const baseWebpack = webpackConfig()

const mergedExternals: ExternalItem[] = []
if (baseWebpack.externals !== undefined) {
if (Array.isArray(baseWebpack.externals)) {
mergedExternals.push(...baseWebpack.externals)
} else {
mergedExternals.push(baseWebpack.externals)
}
}
mergedExternals.push({
'expect': 'commonjs2 expect',
'@jest/expect-utils': 'commonjs2 @jest/expect-utils',
'pretty-format': 'commonjs2 pretty-format',
'jest-diff': 'commonjs2 jest-diff',
})

return (config: any) => {
config.set({
plugins: [
Expand Down Expand Up @@ -56,13 +77,7 @@ export const createKarmaConfig = (
singleRun: !DEBUG_MODE, //set to false to leave electron window open
webpack: {
...baseWebpack,
externals: {
...(baseWebpack.externals ?? {}),
'expect': 'commonjs2 expect',
'@jest/expect-utils': 'commonjs2 @jest/expect-utils',
'pretty-format': 'commonjs2 pretty-format',
'jest-diff': 'commonjs2 jest-diff',
},
externals: mergedExternals,
}
})
}
Expand Down
12 changes: 7 additions & 5 deletions packages/browser-test-runner/src/createWebpackConfig.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import path from 'path'
import webpack from 'webpack'
import webpack, { type Configuration } from 'webpack'
import NodePolyfillPlugin from 'node-polyfill-webpack-plugin'

interface CreateWebpackConfigOptions {
libraryName: string
alias?: Record<string, string>
fallback?: Record<string, string>
externals?: Record<string, string>
alias?: webpack.ResolveOptions['alias']
fallback?: webpack.ResolveOptions['fallback']
externals?: webpack.Externals
}

type CreateWebpackConfigReturnType = () => Configuration

export const createWebpackConfig = (
{ libraryName, alias = {}, fallback = {}, externals = {} }: CreateWebpackConfigOptions
): Record<string, any> => {
): CreateWebpackConfigReturnType => {
return () => {
return {
cache: {
Expand Down
15 changes: 7 additions & 8 deletions packages/dht/karma.config.js → packages/dht/karma.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/* eslint-disable @typescript-eslint/no-require-imports */
const path = require('path')
const { createKarmaConfig, createWebpackConfig } = require('@streamr/browser-test-runner')
import { resolve } from 'path'
import { createKarmaConfig, createWebpackConfig } from '@streamr/browser-test-runner'

const TEST_PATHS = [
'test/unit/**/!(connectivityRequestHandler*).ts',
'./test/integration/**/!(DhtWith*|ReplicateData*|GeoIpConnectivityChecking*).ts/',
'./test/end-to-end/**/!(RecoveryFromFailedAutoCertification*|memory-leak*|GeoIpLayer0*).ts'
]

const NodeWebrtcConnection = path.resolve(__dirname, 'src/connection/webrtc/NodeWebrtcConnection.ts')
const BrowserWebrtcConnection = path.resolve(__dirname, 'src/connection/webrtc/BrowserWebrtcConnection.ts')
const NodeWebsocketClientConnection = path.resolve(__dirname, 'src/connection/websocket/NodeWebsocketClientConnection.ts')
const BrowserWebsocketClientConnection = path.resolve(__dirname, 'src/connection/websocket/BrowserWebsocketClientConnection.ts')
const NodeWebrtcConnection = resolve(__dirname, 'src/connection/webrtc/NodeWebrtcConnection.ts')
const BrowserWebrtcConnection = resolve(__dirname, 'src/connection/webrtc/BrowserWebrtcConnection.ts')
const NodeWebsocketClientConnection = resolve(__dirname, 'src/connection/websocket/NodeWebsocketClientConnection.ts')
const BrowserWebsocketClientConnection = resolve(__dirname, 'src/connection/websocket/BrowserWebsocketClientConnection.ts')

module.exports = createKarmaConfig(
export default createKarmaConfig(
TEST_PATHS,
createWebpackConfig({
libraryName: 'dht',
Expand Down
2 changes: 1 addition & 1 deletion packages/dht/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"clean": "jest --clearCache || true; rm -rf dist *.tsbuildinfo node_modules/.cache || true",
"eslint": "eslint --cache --cache-location=node_modules/.cache/.eslintcache/ '*/**/*.{js,ts}'",
"test": "npm run test-unit && npm run test-integration && npm run test-end-to-end",
"test-browser": "karma start karma.config.js",
"test-browser": "karma start karma.config.ts",
"test-unit": "jest test/unit",
"test-integration": "jest --bail test/integration",
"test-end-to-end": "jest --bail test/end-to-end",
Expand Down
11 changes: 0 additions & 11 deletions packages/proto-rpc/karma.config.js

This file was deleted.

10 changes: 10 additions & 0 deletions packages/proto-rpc/karma.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createKarmaConfig, createWebpackConfig } from '@streamr/browser-test-runner'

const TEST_PATHS = ['test/**/*.ts']

export default createKarmaConfig(TEST_PATHS, createWebpackConfig({
libraryName: 'proto-rpc',
fallback: {
module: false
}
}))
2 changes: 1 addition & 1 deletion packages/proto-rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"clean": "jest --clearCache || true; rm -rf dist *.tsbuildinfo node_modules/.cache || true",
"eslint": "./test-proto.sh && eslint --cache --cache-location=node_modules/.cache/.eslintcache/ '*/**/*.{js,ts}'",
"test": "./test-proto.sh && npm run test-unit && npm run test-integration",
"test-browser": "./test-proto.sh && karma start karma.config.js",
"test-browser": "./test-proto.sh && karma start karma.config.ts",
"test-unit": "./test-proto.sh && jest test/unit",
"test-integration": "./test-proto.sh && jest --bail test/integration"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/sdk/karma-end-to-end.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/sdk/karma-end-to-end.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import webpackConfig from './webpack-karma.config'
import { createKarmaConfig } from '@streamr/browser-test-runner'

export default createKarmaConfig(['test/end-to-end/**/*.ts'], webpackConfig, __dirname)
5 changes: 0 additions & 5 deletions packages/sdk/karma-integration.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/sdk/karma-integration.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import webpackConfig from './webpack-karma.config'
import { createKarmaConfig } from '@streamr/browser-test-runner'

export default createKarmaConfig(['test/integration/**/*.ts'], webpackConfig, __dirname)
7 changes: 3 additions & 4 deletions packages/sdk/karma-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* package. See karma-config.js in that package for more details.
*/

// eslint-disable-next-line @typescript-eslint/no-require-imports
require('./test/test-utils/customMatchers')
import './test/test-utils/customMatchers'
import { customMatchers } from '@streamr/test-utils'

const { customMatchers } = require('@streamr/test-utils')
expect.extend(customMatchers)
expect.extend(customMatchers)
5 changes: 0 additions & 5 deletions packages/sdk/karma-unit.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/sdk/karma-unit.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import webpackConfig from './webpack-karma.config'
import { createKarmaConfig } from '@streamr/browser-test-runner'

export default createKarmaConfig(['test/unit/**/*.ts'], webpackConfig, __dirname)
6 changes: 3 additions & 3 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"test-unit": "jest --useStderr --forceExit test/unit",
"test-integration": "jest --bail --useStderr --forceExit --testTimeout=40000 test/integration/*.test.*",
"test-end-to-end": "jest --bail --useStderr --forceExit --testTimeout=40000 test/end-to-end/*.test.*",
"test-browser-unit": "karma start karma-unit.config.js",
"test-browser-integration": "karma start karma-integration.config.js",
"test-browser-end-to-end": "karma start karma-end-to-end.config.js",
"test-browser-unit": "karma start karma-unit.config.ts",
"test-browser-integration": "karma start karma-integration.config.ts",
"test-browser-end-to-end": "karma start karma-end-to-end.config.ts",
"test-browser-smoke": "bash test/browser-smoke-test/smoke-test.sh",
"test-exports": "cd test/exports && npm run link && tsc --noEmit --project ./tsconfig.json && npm test"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const defaultConfig = require('./webpack.config')
const path = require('path')
import type { Configuration } from 'webpack'
import defaultConfig from './webpack.config'
import path from 'path'

module.exports = (env, argv) => {
const karmaWebpackConfig: (env?: Record<string, unknown>, argv?: Record<string, unknown>) => Configuration = (env = {}, argv = {}) => {
const config = defaultConfig(env, argv)

return {
Expand All @@ -17,6 +18,8 @@ module.exports = (env, argv) => {
v8: false,
'jest-leak-detector': false,
}
}
}
},
} as Configuration

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid this casting?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, by… casting the result of defaultConfig but it makes merging them configs a mess.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that's actually a "no", eh?

}

export default karmaWebpackConfig
2 changes: 2 additions & 0 deletions packages/sdk/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ module.exports = (env, argv) => {
]
},
resolve: {
alias: {},
fallback: {},
modules: ['node_modules', ...require.resolve.paths('')],
extensions: ['.json', '.js', '.ts'],
},
Expand Down
4 changes: 2 additions & 2 deletions packages/trackerless-network/karma-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
* package. See karma-config.js in that package for more details.
*/

const { customMatchers } = require('@streamr/test-utils')
expect.extend(customMatchers)
import { customMatchers } from '@streamr/test-utils'
expect.extend(customMatchers)
30 changes: 0 additions & 30 deletions packages/trackerless-network/karma.config.js

This file was deleted.

29 changes: 29 additions & 0 deletions packages/trackerless-network/karma.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { resolve } from 'path'
import { createKarmaConfig, createWebpackConfig } from '@streamr/browser-test-runner'

const TEST_PATHS = [
'./test/unit/**/*.ts',
'./test/integration/**/*.ts',
'./test/end-to-end/**/!(webrtc*|websocket*)',
]

export default createKarmaConfig(TEST_PATHS, createWebpackConfig({
libraryName: 'trackerless-network',
alias: {
[resolve(__dirname, '../dht/src/connection/webrtc/NodeWebrtcConnection.ts')]:
resolve(__dirname, '../dht/src/connection/webrtc/BrowserWebrtcConnection.ts'),
[resolve(__dirname, '../dht/src/connection/websocket/NodeWebsocketClientConnection.ts')]:
resolve(__dirname, '../dht/src/connection/websocket/BrowserWebsocketClientConnection.ts'),
'@streamr/dht': resolve('../dht/src/exports.ts'),
'@streamr/proto-rpc': resolve('../proto-rpc/src/exports.ts'),
},
fallback: {
module: false
},
externals: {
'node-datachannel': 'commonjs node-datachannel',
express: 'Express',
http: 'HTTP',
ws: 'WebSocket',
}
}), __dirname)
2 changes: 1 addition & 1 deletion packages/trackerless-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"coverage": "jest --coverage",
"eslint": "eslint --cache --cache-location=node_modules/.cache/.eslintcache/ '*/**/*.{js,ts}'",
"test": "npm run test-unit && npm run test-integration && npm run test-end-to-end",
"test-browser": "karma start karma.config.js",
"test-browser": "karma start karma.config.ts",
"test-unit": "jest test/unit",
"test-integration": "jest --bail test/integration",
"test-end-to-end": "jest --bail test/end-to-end",
Expand Down
11 changes: 0 additions & 11 deletions packages/utils/karma.config.js

This file was deleted.

10 changes: 10 additions & 0 deletions packages/utils/karma.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createKarmaConfig, createWebpackConfig } from '@streamr/browser-test-runner'

const TEST_PATHS = ['test/**/*.ts']

export default createKarmaConfig(TEST_PATHS, createWebpackConfig({
libraryName: 'utils',
fallback: {
module: false
}
}))
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"clean": "jest --clearCache || true; rm -rf dist *.tsbuildinfo node_modules/.cache || true",
"eslint": "eslint --cache --cache-location=node_modules/.cache/.eslintcache/ '*/**/*.{js,ts}'",
"test": "jest",
"test-browser": "karma start karma.config.js"
"test-browser": "karma start karma.config.ts"
},
"author": "Streamr Network AG <contact@streamr.network>",
"license": "Apache-2.0",
Expand Down