-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create
copyAssetsConcurrently
and add more tests
- Loading branch information
Showing
3 changed files
with
122 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,107 @@ | ||
import memfs, { vol } from 'memfs'; | ||
|
||
import { copyAssets } from './assets'; | ||
import { copyAssets, copyAssetsConcurrently } from './assets'; | ||
|
||
jest.mock('fs', () => memfs); | ||
jest.mock('fs-extra', () => memfs); | ||
|
||
jest | ||
.spyOn(console, 'log') | ||
.mockImplementation((...args) => stdoutMock(`${args.join(' ')}\n`)); | ||
|
||
const stdoutMock = jest.fn().mockName('[stdout]'); | ||
const getStdOut = () => `${stdoutMock.name}${stdoutMock.mock.calls.join('')}`; | ||
|
||
// a snapshot serializer to remove quotes around stdout | ||
expect.addSnapshotSerializer({ | ||
test: (val) => typeof val === 'string' && val.startsWith(stdoutMock.name), | ||
print: (val) => (val as string).trim().replace(stdoutMock.name, ''), | ||
}); | ||
|
||
// the glob in package.json breaks syntax highlighting in VS Code | ||
const justOutDirs = (fs: typeof vol) => | ||
Object.fromEntries( | ||
Object.entries(fs.toJSON(['.'], {}, true)).filter( | ||
([key]) => !(key.includes('package.json') || key.includes('src/')), | ||
), | ||
); | ||
|
||
beforeEach(() => { | ||
vol.reset(); | ||
vol.fromJSON({ | ||
'package.json': JSON.stringify({ | ||
skuba: { | ||
assets: ['**/*.vocab/*trans.json'], | ||
assets: ['**/*.vocab/*translations.json'], | ||
}, | ||
}), | ||
'src/app.ts': '', | ||
'src/.vocab/index.ts': '', | ||
'src/.vocab/trans.json': '', | ||
'src/.vocab/id.trans.json': '', | ||
'src/.vocab/th.trans.json': '', | ||
'src/.vocab/translations.json': '', | ||
'src/.vocab/th.translations.json': '', | ||
'src/other.vocab/index.ts': '', | ||
'src/other.vocab/trans.json': '', | ||
'src/other.vocab/id.trans.json': '', | ||
'src/other.vocab/th.trans.json': '', | ||
'src/other.vocab/translations.json': '', | ||
'src/other.vocab/th.translations.json': '', | ||
}); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('copyAssets', () => { | ||
it('should copy the specified assets to the destination directory', async () => { | ||
const { log } = jest.createMockFromModule< | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-imports | ||
typeof import('../../utils/logging') | ||
>('../../utils/logging'); | ||
await copyAssets('lib', log); | ||
|
||
expect(vol.toJSON(['.'], {}, true)).toMatchInlineSnapshot(` | ||
it('should copy the assets specified in skuba config to the destination directory', async () => { | ||
await copyAssets('lib'); | ||
|
||
expect(justOutDirs(vol)).toMatchInlineSnapshot(` | ||
{ | ||
"lib/.vocab/id.trans.json": "", | ||
"lib/.vocab/th.trans.json": "", | ||
"lib/.vocab/trans.json": "", | ||
"lib/other.vocab/id.trans.json": "", | ||
"lib/other.vocab/th.trans.json": "", | ||
"lib/other.vocab/trans.json": "", | ||
"package.json": "{"skuba":{"assets":["**/*.vocab/*trans.json"]}}", | ||
"src/.vocab/id.trans.json": "", | ||
"src/.vocab/index.ts": "", | ||
"src/.vocab/th.trans.json": "", | ||
"src/.vocab/trans.json": "", | ||
"src/app.ts": "", | ||
"src/other.vocab/id.trans.json": "", | ||
"src/other.vocab/index.ts": "", | ||
"src/other.vocab/th.trans.json": "", | ||
"src/other.vocab/trans.json": "", | ||
"lib/.vocab/th.translations.json": "", | ||
"lib/.vocab/translations.json": "", | ||
"lib/other.vocab/th.translations.json": "", | ||
"lib/other.vocab/translations.json": "", | ||
} | ||
`); | ||
expect(getStdOut()).toMatchInlineSnapshot(` | ||
Copying .vocab/th.translations.json | ||
Copying .vocab/translations.json | ||
Copying other.vocab/th.translations.json | ||
Copying other.vocab/translations.json | ||
`); | ||
}); | ||
}); | ||
|
||
describe('copyAssetsConcurrently', () => { | ||
it('should copy the assets specified in skuba config to the destination directories', async () => { | ||
await copyAssetsConcurrently([ | ||
{ | ||
outDir: 'lib-commonjs', | ||
name: 'commonjs', | ||
prefixColor: 'green', | ||
}, | ||
{ | ||
outDir: 'lib-es2015', | ||
name: 'es2015', | ||
prefixColor: 'yellow', | ||
}, | ||
]); | ||
|
||
expect(justOutDirs(vol)).toMatchInlineSnapshot(` | ||
{ | ||
"lib-commonjs/.vocab/th.translations.json": "", | ||
"lib-commonjs/.vocab/translations.json": "", | ||
"lib-commonjs/other.vocab/th.translations.json": "", | ||
"lib-commonjs/other.vocab/translations.json": "", | ||
"lib-es2015/.vocab/th.translations.json": "", | ||
"lib-es2015/.vocab/translations.json": "", | ||
"lib-es2015/other.vocab/th.translations.json": "", | ||
"lib-es2015/other.vocab/translations.json": "", | ||
} | ||
`); | ||
expect(getStdOut()).toMatchInlineSnapshot(` | ||
commonjs │ Copying .vocab/th.translations.json | ||
commonjs │ Copying .vocab/translations.json | ||
commonjs │ Copying other.vocab/th.translations.json | ||
commonjs │ Copying other.vocab/translations.json | ||
es2015 │ Copying .vocab/th.translations.json | ||
es2015 │ Copying .vocab/translations.json | ||
es2015 │ Copying other.vocab/th.translations.json | ||
es2015 │ Copying other.vocab/translations.json | ||
`); | ||
}); | ||
}); |
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