This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acee839
commit dca9764
Showing
6 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,16 @@ | ||
import type fs from 'fs/promises' | ||
|
||
/** | ||
* @file Node Module Mock - fs/promises | ||
* @module tests/mocks/fs/promises | ||
* @see https://jestjs.io/docs/manual-mocks#mocking-node-modules | ||
* @see https://jestjs.io/docs/manual-mocks#examples | ||
* @see https://github.com/nodejs/node/blob/v16.0.0/doc/api/fs.md | ||
*/ | ||
|
||
export default { | ||
chmod: jest.fn(), | ||
stat: jest.fn(), | ||
unlink: jest.fn(), | ||
writeFile: jest.fn() | ||
} as jest.Mocked<Partial<typeof fs>> |
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,8 @@ | ||
/** | ||
* @file Node Module Mock - mkdirp | ||
* @module tests/mocks/mkdirp | ||
* @see https://jestjs.io/docs/manual-mocks#mocking-node-modules | ||
* @see https://github.com/isaacs/node-mkdirp | ||
*/ | ||
|
||
export default jest.fn() |
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,33 @@ | ||
import fs from 'fs/promises' | ||
import mkdirp from 'mkdirp' | ||
import path from 'path' | ||
import testSubject from '../save-file.util' | ||
|
||
/** | ||
* @file Functional Tests - saveFile | ||
* @module trext/utils/tests/functional/saveFile | ||
*/ | ||
|
||
jest.mock('fs/promises') | ||
|
||
const mockFs = fs as jest.Mocked<typeof fs> | ||
const mockMkdirp = mkdirp as jest.MockedFunction<typeof mkdirp> | ||
|
||
describe('functional:utils/saveFile', () => { | ||
const FILENAME: string = 'dist/hello-world.mjs' | ||
const DATA: Buffer = Buffer.from("console.log('Hello, World!')") | ||
|
||
beforeEach(async () => { | ||
await testSubject(FILENAME, DATA) | ||
}) | ||
|
||
it('should handle directory creation', () => { | ||
expect(mockMkdirp).toBeCalledTimes(1) | ||
expect(mockMkdirp).toBeCalledWith(path.dirname(FILENAME)) | ||
}) | ||
|
||
it('should create new file', () => { | ||
expect(mockFs.writeFile).toBeCalledTimes(1) | ||
expect(mockFs.writeFile).toBeCalledWith(FILENAME, DATA) | ||
}) | ||
}) |
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,26 @@ | ||
import fs from 'fs/promises' | ||
import mkdirp from 'mkdirp' | ||
import path from 'path' | ||
|
||
/** | ||
* @file Utilities - saveFile | ||
* @module trext/utils/saveFile | ||
*/ | ||
|
||
/** | ||
* Saves a file. Creates a new directory if necessary. | ||
* | ||
* @async | ||
* @param {string} filename - Name of file to save | ||
* @param {string | Buffer} data - File content | ||
* @return {Promise<void>} Empty promise when complete | ||
*/ | ||
const saveFile = async ( | ||
filename: string, | ||
data: string | Buffer | ||
): Promise<void> => { | ||
await mkdirp(path.dirname(filename)) | ||
return await fs.writeFile(filename, data) | ||
} | ||
|
||
export default saveFile |