refactor: mockless tests #163

Merged
mcataford merged 8 commits from mockless-tests into master 2021-02-19 04:31:33 +00:00
Showing only changes of commit 49e1d798c8 - Show all commits

View file

@ -1,11 +1,13 @@
import * as childProcess from 'child_process'
import { readFileSync } from 'fs'
import { promises as fs, readFileSync } from 'fs'
import { tmpdir } from 'os'
import { join, resolve } from 'path'
import mockFS from 'mock-fs'
import runPackwatch from '..'
jest.mock('child_process')
//jest.mock('child_process')
function getPackOutput({ packageSize, unpackedSize }) {
return `
@ -28,11 +30,26 @@ npm notice
`
}
async function prepareWorkspace(): Promise<string> {
const workspacePath = await fs.mkdtemp(`${tmpdir()}/`, { recursive: true })
return workspacePath
}
async function cleanUpWorkspace(paths: string[]): Promise<void> {
return Promise.all(
paths.map(async path => fs.rmdir(path, { recursive: true })),
)
}
async function createFile(path: string, content: string): Promise<void> {
await fs.writeFile(path, content)
}
/*
function getManifest() {
try {
return JSON.parse(readFileSync('.packwatch.json', { encoding: 'utf8' }))
return JSON.parse(readFileSync(".packwatch.json", { encoding: "utf8" }))
} catch {
/* No manifest */
}
}
@ -44,11 +61,11 @@ function setupMockFS({
}) {
const fs = {}
if (hasPackageJSON) fs['package.json'] = '{}'
if (hasPackageJSON) fs["package.json"] = "{}"
if (hasManifest)
fs['.packwatch.json'] = JSON.stringify({
unpackedSize: '0.5 B',
fs[".packwatch.json"] = JSON.stringify({
unpackedSize: "0.5 B",
limitBytes: manifestLimit,
limit: `${manifestLimit} B`,
packageSize: `${manifestSize} B`,
@ -56,20 +73,31 @@ function setupMockFS({
})
mockFS(fs)
}
*/
describe('Packwatch', () => {
let mockLogger
let workspacePath
beforeEach(() => {
mockFS({})
mockLogger = jest.spyOn(global.console, 'log').mockImplementation()
//mockFS({})
//mockLogger = jest.spyOn(global.console, 'log').mockImplementation()
})
afterEach(jest.restoreAllMocks)
afterEach(async () => {
jest.restoreAllMocks()
if (workspacePath) {
await cleanUpWorkspace([workspacePath])
workspacePath = null
}
})
/*
afterAll(mockFS.restore)
*/
it('warns the user and errors if run away from package.json', () => {
mockFS({})
runPackwatch()
it.skip('warns the user and errors if run away from package.json', async () => {
workspacePath = await prepareWorkspace()
runPackwatch({ cwd: workspacePath })
expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toMatchInlineSnapshot(
@ -78,20 +106,42 @@ describe('Packwatch', () => {
})
describe('without manifest', () => {
/*
beforeEach(() => {
setupMockFS({ hasPackageJSON: true })
})
*/
it.each(['1 B', '1.1 B', '1 kB', '1.1 kB', '1 mB', '1.1 mB'])(
'generates the initial manifest properly (size = %s)',
mockSize => {
jest.spyOn(childProcess, 'spawnSync').mockReturnValue({
it('generates the initial manifest properly', async () => {
workspacePath = await prepareWorkspace()
await createFile(
resolve(join(workspacePath, 'package.json')),
'{ "name": "wow", "version": "0.0.0" }',
)
runPackwatch({ cwd: workspacePath })
const generatedManifest = await fs.readFile(
resolve(join(workspacePath, '.packwatch.json')),
{ encoding: 'utf8' },
)
expect(generatedManifest).toMatchInlineSnapshot(
'"{\\"limit\\":\\"138 B\\",\\"packageSize\\":\\"138 B\\",\\"unpackedSize\\":\\"37 B\\"}"',
)
})
/*it.each(["1 B", "1.1 B", "1 kB", "1.1 kB", "1 mB", "1.1 mB"])(
"generates the initial manifest properly (size = %s)",
(mockSize) => {
jest.spyOn(childProcess, "spawnSync").mockReturnValue({
stderr: getPackOutput({
packageSize: mockSize,
unpackedSize: mockSize,
}),
})
const returnCode = runPackwatch()
const returnCode = runPackwatch({})
const manifest = getManifest()
expect(returnCode).toEqual(1)
expect(manifest).toEqual({
@ -99,10 +149,10 @@ describe('Packwatch', () => {
packageSize: mockSize,
unpackedSize: mockSize,
})
},
)
}
)*/
it('outputs expected messaging', () => {
it.skip('outputs expected messaging', () => {
jest.spyOn(childProcess, 'spawnSync').mockReturnValue({
stderr: getPackOutput({
packageSize: '1 B',
@ -110,7 +160,7 @@ describe('Packwatch', () => {
}),
})
runPackwatch()
runPackwatch({})
expect(mockLogger.mock.calls).toHaveLength(2)
expect(mockLogger.mock.calls[0][0]).toMatchInlineSnapshot(`
@ -122,7 +172,7 @@ describe('Packwatch', () => {
)
})
it('outputs expected messaging when not updating the manifest', () => {
it.skip('outputs expected messaging when not updating the manifest', () => {
jest.spyOn(childProcess, 'spawnSync').mockReturnValue({
stderr: getPackOutput({
packageSize: '1 B',
@ -140,7 +190,7 @@ describe('Packwatch', () => {
})
})
describe('with manifest', () => {
describe.skip('with manifest', () => {
it('messages when the size is equal to the limit', () => {
setupMockFS({
hasPackageJSON: true,
@ -154,7 +204,7 @@ describe('Packwatch', () => {
unpackedSize: '2 B',
}),
})
runPackwatch()
runPackwatch({})
expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toMatchInlineSnapshot(
'"📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: 1 B)"',
@ -174,7 +224,7 @@ describe('Packwatch', () => {
unpackedSize: '2 B',
}),
})
runPackwatch()
runPackwatch({})
expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toMatchInlineSnapshot(
'"📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: 5 B)"',
@ -193,7 +243,7 @@ describe('Packwatch', () => {
unpackedSize: '2 B',
}),
})
runPackwatch()
runPackwatch({})
expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toMatchInlineSnapshot(
'"📦 👀 Your package grew! 3 B > 2 B (Limit: 5 B)"',
@ -212,7 +262,7 @@ describe('Packwatch', () => {
unpackedSize: '2 B',
}),
})
runPackwatch()
runPackwatch({})
expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toMatchInlineSnapshot(
'"📦 💯 Your package shrank! 1 B < 2 B (Limit: 5 B)"',
@ -231,7 +281,7 @@ describe('Packwatch', () => {
unpackedSize: '2 B',
}),
})
runPackwatch()
runPackwatch({})
expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toMatchInlineSnapshot(`
"🔥🔥📦🔥🔥 Your package exceeds the limit set in .packwatch.json! 1 B > 0.5 B