Skip to content

Commit 8586aa4

Browse files
author
William Luke
authored
Merge pull request #18 from JoseRFelix/test/add-general-testing
2 parents 5c431ae + d78542d commit 8586aa4

File tree

5 files changed

+2115
-56
lines changed

5 files changed

+2115
-56
lines changed

jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
roots: ['<rootDir>/src'],
3+
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
4+
transform: {
5+
'^.+\\.(ts|tsx)$': 'ts-jest',
6+
},
7+
}

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,23 @@
3030
"dev:test-app": "cd test-app && yarn dev",
3131
"dev": "yarn watch & yarn dev:test-app",
3232
"build": "rimraf ./dist && tsc",
33-
"stable": "dripip stable"
33+
"stable": "dripip stable",
34+
"test": "jest",
35+
"test:watch": "jest --watch"
3436
},
3537
"dependencies": {},
3638
"devDependencies": {
3739
"@prisma-labs/prettier-config": "0.1.0",
40+
"@types/jest": "^26.0.15",
3841
"@types/webpack": "4.41.22",
3942
"dripip": "0.10.0",
4043
"eslint": "7.10.0",
4144
"eslint-config-prettier": "6.12.0",
45+
"jest": "^26.6.0",
4246
"prettier": "2.1.2",
4347
"prettier-eslint": "11.0.0",
4448
"rimraf": "^3.0.2",
49+
"ts-jest": "^26.4.1",
4550
"typescript": "4.0.3",
4651
"webpack": "4.44.2"
4752
},

src/__tests__/index.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import withPrismaPlugin from '../index'
2+
3+
describe('withPrismaPlugin', () => {
4+
it('should keep nextConfig object properties in other phases', () => {
5+
const nextConfig = { foo: 'bar' }
6+
7+
const newConfig = withPrismaPlugin(nextConfig)('phase-export', {})
8+
expect(newConfig).toEqual(nextConfig)
9+
})
10+
11+
it('should call nextConfig function in other phases', () => {
12+
const nextConfig = () => ({ foo: 'bar' })
13+
14+
const newConfig = withPrismaPlugin(nextConfig)('phase-export', {})
15+
expect(newConfig).toEqual(nextConfig())
16+
})
17+
18+
it('should add webpack property', () => {
19+
const nextConfig = { foo: 'bar' }
20+
21+
const newConfig = withPrismaPlugin(nextConfig)('phase-development-server', {})
22+
expect(newConfig).toHaveProperty('webpack')
23+
})
24+
})

src/index.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,39 @@ const PATH_DELIMITER = '[\\\\/]' // match 2 antislashes or one slash
66
* paths of the modules. So we need to check for \\ and /
77
*/
88
// @ts-ignore
9-
const safePath = module => module.split(/[\\\/]/g).join(PATH_DELIMITER)
9+
const safePath = (module) => module.split(/[\\\/]/g).join(PATH_DELIMITER)
1010

1111
/**
1212
* Actual Next.js plugin
1313
*/
1414

1515
const withPrismaPlugin = (nextConfig = {}) => (
16-
phase:
17-
| "phase-export"
18-
| "phase-production-build"
19-
| "phase-production-server"
20-
| "phase-development-server",
21-
thing: any
16+
phase: 'phase-export' | 'phase-production-build' | 'phase-production-server' | 'phase-development-server',
17+
thing: any
2218
) => {
23-
if (phase === "phase-development-server") {
19+
if (phase === 'phase-development-server') {
2420
return Object.assign({}, nextConfig, {
2521
webpack(config: any, options: any) {
2622
const ignore = ['.prisma/client', '@prisma/client']
2723

2824
// const includes = ignore.map(module => (new RegExp(`${module}(?!.*node_modules)`)));
29-
const excludes = [new RegExp(`node_modules(?!/(${ignore.join('|')})(?!.*node_modules))`)];
30-
const ignored = config.watchOptions.ignored.filter(
31-
(ignored: string) => ignored !== '**/node_modules/**'
32-
).concat(excludes);
25+
const excludes = [new RegExp(`node_modules(?!/(${ignore.join('|')})(?!.*node_modules))`)]
26+
const ignored = config.watchOptions.ignored
27+
.filter((ignored: string) => ignored !== '**/node_modules/**')
28+
.concat(excludes)
3329
return Object.assign(config, {
3430
plugins: [...config.plugins, new PrismaClientReloaderWebpackPlugin()],
3531
watchOptions: {
3632
...config.watchOptions,
37-
ignored
38-
}
33+
ignored,
34+
},
3935
})
40-
}
36+
},
4137
})
4238
}
43-
let internalConfigObj =
44-
typeof nextConfig === "function"
45-
? nextConfig(phase, thing)
46-
: nextConfig;
47-
return internalConfigObj;
39+
let internalConfigObj = typeof nextConfig === 'function' ? nextConfig(phase, thing) : nextConfig
40+
return internalConfigObj
4841
}
4942

5043
module.exports = withPrismaPlugin
44+
export default withPrismaPlugin

0 commit comments

Comments
 (0)