Skip to content

Commit 3ce70f0

Browse files
committed
test: add tests for new feature
1 parent ef15240 commit 3ce70f0

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed

src/dependencies.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import test from 'ava'
2+
import { join } from 'path'
3+
import { findPackagePaths, findDependencies } from './dependencies'
4+
5+
const path = (...paths: string[]): string => join(__dirname, ...paths)
6+
7+
test.serial('finds package paths in git monorepo', async t => {
8+
const cwd = path('fixtures/monorepo/packages/package')
9+
process.chdir(cwd)
10+
11+
const packagePaths = [
12+
path('fixtures/monorepo/packages/package/package.json'),
13+
path('fixtures/monorepo/package.json')
14+
]
15+
16+
// Should have two assertions
17+
t.plan(2)
18+
19+
for await (const packagePath of findPackagePaths()) {
20+
t.is(packagePath, packagePaths.shift())
21+
}
22+
})
23+
24+
test.serial('finds package paths in non-git monorepo', async t => {
25+
const cwd = path('fixtures/monorepo-no-git/packages/package')
26+
process.chdir(cwd)
27+
28+
const packagePaths = [
29+
path('fixtures/monorepo-no-git/packages/package/package.json'),
30+
path('fixtures/monorepo-no-git/package.json'),
31+
path('../package.json')
32+
]
33+
34+
// Should have three assertions
35+
t.plan(3)
36+
37+
for await (const packagePath of findPackagePaths()) {
38+
t.is(packagePath, packagePaths.shift())
39+
}
40+
})
41+
42+
test.serial('finds dependencies in monorepo', async t => {
43+
const cwd = path('fixtures/monorepo/packages/package')
44+
process.chdir(cwd)
45+
46+
const dependencies = await findDependencies({
47+
packagePaths: findPackagePaths(),
48+
keys: ['dependencies'],
49+
warnings: []
50+
})
51+
52+
t.deepEqual(new Set(dependencies), new Set(['lodash', 'express', 'chalk']))
53+
})
54+
55+
test.serial('finds dev dependencies in monorepo', async t => {
56+
const cwd = path('fixtures/monorepo/packages/package')
57+
process.chdir(cwd)
58+
59+
const devDependencies = await findDependencies({
60+
packagePaths: findPackagePaths(),
61+
keys: ['devDependencies'],
62+
warnings: []
63+
})
64+
65+
t.deepEqual(new Set(devDependencies), new Set(['typescript', 'rollup']))
66+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
3+
}

src/fixtures/monorepo/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"lodash": "^4.17.15",
4+
"chalk": "^4.0.0"
5+
},
6+
"devDependencies": {
7+
"typescript": "^3.9.2",
8+
"rollup": "^2.10.2"
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"dependencies": {
3+
"lodash": "^4.17.15",
4+
"express": "4.17.1"
5+
},
6+
"peerDependencies": {
7+
"@babel/core": "^7.0.0"
8+
}
9+
}

src/index.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import test from 'ava'
22
import { testProp, fc } from 'ava-fast-check'
33
import { Arbitrary } from 'fast-check'
4-
import { PluginContext, Plugin } from 'rollup'
4+
import { PluginContext, Plugin, InputOptions } from 'rollup'
55
import externals, { ExternalsOptions } from './index'
6+
import { join } from "path"
67

78
// Returns an arbitrary for generating externals options objects
89
const externalsOptionsArbitrary = (): Arbitrary<ExternalsOptions> =>
@@ -31,10 +32,30 @@ testProp(
3132
}
3233
)
3334

34-
test('does not mark "dependencies" dependency as external by default', t => {
35+
test('does not mark "dependencies" dependency as external by default', async t => {
3536
const source = 'example'
3637
const importer = 'me'
37-
const plugin = externals({ packagePath: './fixtures/test.json' }) as Plugin & PluginContext
38+
const plugin = externals({ packagePath: './fixtures/test.json' }) as Required<Plugin> & PluginContext
3839

40+
await plugin.buildStart({} as InputOptions)
3941
t.is(plugin.resolveId(source, importer), null)
4042
})
43+
44+
const path = (...paths: string[]): string => join(__dirname, ...paths)
45+
46+
test.serial('monorepo usage', async t => {
47+
const cwd = path('fixtures/monorepo/packages/package')
48+
process.chdir(cwd)
49+
50+
const importer = 'me'
51+
const plugin = externals() as Required<Plugin> & PluginContext
52+
await plugin.buildStart({} as InputOptions)
53+
54+
for (const source of ['@babel/core', 'typescript', 'rollup']) {
55+
t.false(plugin.resolveId(source, importer))
56+
}
57+
58+
for (const source of ['lodash', 'express', 'chalk']) {
59+
t.is(plugin.resolveId(source, importer), null)
60+
}
61+
})

0 commit comments

Comments
 (0)