From 70ab606ffdfbbc9965f744950fb9c0b02d9c1d67 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Thu, 3 Jun 2021 23:47:09 -0400 Subject: [PATCH] refactor: test, types --- jest.config.js | 2 +- package.json | 4 +- src/index.ts | 5 +- src/{index.d.ts => types.ts} | 0 src/utils.ts | 2 +- {src/__tests__ => tests}/index.test.ts | 73 +++++++++++++++----------- {src/__tests__ => tests}/utils.test.ts | 2 +- tsconfig.json | 2 +- 8 files changed, 50 insertions(+), 40 deletions(-) rename src/{index.d.ts => types.ts} (100%) rename {src/__tests__ => tests}/index.test.ts (80%) rename {src/__tests__ => tests}/utils.test.ts (88%) diff --git a/jest.config.js b/jest.config.js index be1ab47..d77bfb5 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,6 +3,6 @@ module.exports = { transform: { '^.+\\.ts$': 'ts-jest', }, - roots: ['/src'], + roots: ['/tests'], testMatch: ['**/*.test.ts'], } diff --git a/package.json b/package.json index 9bddd23..b53d0a1 100644 --- a/package.json +++ b/package.json @@ -30,9 +30,9 @@ "prepack": "yarn build", "prebuild": "rm -rf dist", "build": "tsc --project .", - "lint": "eslint src/**/*.ts", + "lint": "eslint tests/**/*.ts src/**/*.ts", "lint:fix": "yarn lint --fix", - "test": "jest src", + "test": "jest tests", "test:watch": "yarn test --watchAll", "test:coverage": "yarn test --coverage", "types": "tsc --noEmit src/**/*.ts", diff --git a/src/index.ts b/src/index.ts index 4d401af..af38b04 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import { getPreviousPackageStats, mergeDefaultArguments, } from './utils' -import type { PackwatchArguments } from './index.d' +import type { PackwatchArguments } from './types' import { assertInPackageRoot } from './invariants' import logger from './logger' @@ -52,8 +52,7 @@ export default async function packwatch( limit, limitBytes, } = previousStats - const hasExceededLimit = - packageSizeBytes > (limitBytes ?? Number.MAX_SAFE_INTEGER) + const hasExceededLimit = limitBytes && packageSizeBytes > limitBytes /* * If we are updating the manifest, we can write right away and terminate. diff --git a/src/index.d.ts b/src/types.ts similarity index 100% rename from src/index.d.ts rename to src/types.ts diff --git a/src/utils.ts b/src/utils.ts index 3323586..6d1f011 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,7 +2,7 @@ import { spawnSync } from 'child_process' import { readFileSync, writeFileSync } from 'fs' import { join, resolve } from 'path' -import { PackwatchArguments, Report } from './index.d' +import { PackwatchArguments, Report } from './types' const PACKAGE_SIZE_PATT = /package size:\s*([0-9]+\.?[0-9]*\s+[A-Za-z]{1,2})/ const UNPACKED_SIZE_PATT = /unpacked size:\s*([0-9]+\.?[0-9]*\s+[A-Za-z]{1,2})/ diff --git a/src/__tests__/index.test.ts b/tests/index.test.ts similarity index 80% rename from src/__tests__/index.test.ts rename to tests/index.test.ts index 2f32137..81ec2fe 100644 --- a/src/__tests__/index.test.ts +++ b/tests/index.test.ts @@ -2,11 +2,14 @@ import { promises as fs } from 'fs' import { tmpdir } from 'os' import { join, resolve } from 'path' -import type { Report } from '../index.d' -import packwatch from '..' +import type { Report } from '../src/types' +import packwatch from '../src' + +let workspace: string | null async function prepareWorkspace(): Promise { const workspacePath = await fs.mkdtemp(`${tmpdir()}/`) + workspace = workspacePath return workspacePath } @@ -35,28 +38,21 @@ async function createManifest( const path = resolve(join(cwd, '.packwatch.json')) await createFile(path, JSON.stringify(configuration)) } -describe('Packwatch', () => { - let mockLogger = jest.fn() - let mockWarn = jest.fn() - let mockError = jest.fn() - let workspacePath - beforeEach(() => { - mockLogger = jest.spyOn(console, 'log').mockImplementation() - mockWarn = jest.spyOn(console, 'warn').mockImplementation() - mockError = jest.spyOn(console, 'error').mockImplementation() - }) +describe('Packwatch', () => { afterEach(async () => { jest.restoreAllMocks() - if (workspacePath) { - await cleanUpWorkspace([workspacePath]) - workspacePath = null + if (workspace) { + await cleanUpWorkspace([workspace]) + workspace = null } }) it('warns the user and errors if run away from package.json', async () => { - workspacePath = await prepareWorkspace() + const workspacePath = await prepareWorkspace() + const mockLogger = jest.spyOn(console, 'log') + await expect(async () => packwatch({ cwd: workspacePath }), ).rejects.toThrow('NOT_IN_PACKAGE_ROOT') @@ -71,7 +67,7 @@ describe('Packwatch', () => { describe('without manifest', () => { it('generates the initial manifest properly', async () => { - workspacePath = await prepareWorkspace() + const workspacePath = await prepareWorkspace() await createPackageJson(workspacePath) await expect(async () => @@ -89,7 +85,9 @@ describe('Packwatch', () => { }) it('outputs expected messaging', async () => { - workspacePath = await prepareWorkspace() + const workspacePath = await prepareWorkspace() + const mockWarn = jest.spyOn(console, 'warn') + const mockError = jest.spyOn(console, 'error') await createPackageJson(workspacePath) await expect(async () => @@ -111,7 +109,8 @@ describe('Packwatch', () => { }) it('outputs expected messaging when not updating the manifest', async () => { - workspacePath = await prepareWorkspace() + const mockWarn = jest.spyOn(console, 'warn') + const workspacePath = await prepareWorkspace() await createPackageJson(workspacePath) @@ -128,13 +127,15 @@ describe('Packwatch', () => { describe('with manifest', () => { it('messages when the size is equal to the limit', async () => { - workspacePath = await prepareWorkspace() - + const workspacePath = await prepareWorkspace() + const mockLogger = jest.spyOn(console, 'log') await createPackageJson(workspacePath) await createManifest(workspacePath, { limit: '160B', packageSize: '160B', + packageSizeBytes: 160, unpackedSize: '150B', + unpackedSizeBytes: 150, }) await packwatch({ cwd: workspacePath }) expect(mockLogger.mock.calls).toHaveLength(1) @@ -146,13 +147,15 @@ describe('Packwatch', () => { }) it('messages when the size is lower than the limit (no growth)', async () => { - workspacePath = await prepareWorkspace() - + const workspacePath = await prepareWorkspace() + const mockLogger = jest.spyOn(console, 'log') await createPackageJson(workspacePath) await createManifest(workspacePath, { limit: '170B', packageSize: '160B', + packageSizeBytes: 160, unpackedSize: '150B', + unpackedSizeBytes: 150, }) await packwatch({ cwd: workspacePath }) @@ -164,13 +167,15 @@ describe('Packwatch', () => { ) }) it('messages when the size is lower than the limit (growth)', async () => { - workspacePath = await prepareWorkspace() - + const workspacePath = await prepareWorkspace() + const mockLogger = jest.spyOn(console, 'log') await createPackageJson(workspacePath) await createManifest(workspacePath, { limit: '180B', packageSize: '150B', + packageSizeBytes: 150, unpackedSize: '140B', + unpackedSizeBytes: 140, }) await packwatch({ cwd: workspacePath }) @@ -182,13 +187,15 @@ describe('Packwatch', () => { ) }) it('messages when the size is lower than the limit (shrinkage)', async () => { - workspacePath = await prepareWorkspace() - + const workspacePath = await prepareWorkspace() + const mockLogger = jest.spyOn(console, 'log') await createPackageJson(workspacePath) await createManifest(workspacePath, { limit: '180B', packageSize: '170B', + packageSizeBytes: 170, unpackedSize: '140B', + unpackedSizeBytes: 140, }) await packwatch({ cwd: workspacePath }) @@ -200,13 +207,15 @@ describe('Packwatch', () => { ) }) it('messages when the size exceeds the limit', async () => { - workspacePath = await prepareWorkspace() - + const workspacePath = await prepareWorkspace() + const mockError = jest.spyOn(console, 'error') await createPackageJson(workspacePath) await createManifest(workspacePath, { limit: '10B', packageSize: '170B', + packageSizeBytes: 170, unpackedSize: '140B', + unpackedSizeBytes: 140, }) await expect(async () => @@ -221,13 +230,15 @@ describe('Packwatch', () => { }) it('messages when updating the manifest', async () => { - workspacePath = await prepareWorkspace() - + const workspacePath = await prepareWorkspace() + const mockLogger = jest.spyOn(console, 'log') await createPackageJson(workspacePath) await createManifest(workspacePath, { limit: '10B', packageSize: '170B', + packageSizeBytes: 170, unpackedSize: '140B', + unpackedSizeBytes: 140, }) await packwatch({ cwd: workspacePath, isUpdatingManifest: true }) diff --git a/src/__tests__/utils.test.ts b/tests/utils.test.ts similarity index 88% rename from src/__tests__/utils.test.ts rename to tests/utils.test.ts index f7cc2f9..d3075f1 100644 --- a/src/__tests__/utils.test.ts +++ b/tests/utils.test.ts @@ -1,4 +1,4 @@ -import { convertSizeToBytes } from '../utils' +import { convertSizeToBytes } from '../src/utils' describe('utils', () => { it.each` diff --git a/tsconfig.json b/tsconfig.json index 98eb93a..32d5036 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -69,5 +69,5 @@ "skipLibCheck": true, /* Skip type checking of declaration files. */ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ }, - "exclude": ["dist/**/*", "src/__tests__/**/*"] + "exclude": ["dist/**/*", "tests/**/*"] }