refactor: test, types
This commit is contained in:
parent
242c24d085
commit
70ab606ffd
8 changed files with 50 additions and 40 deletions
|
@ -3,6 +3,6 @@ module.exports = {
|
||||||
transform: {
|
transform: {
|
||||||
'^.+\\.ts$': 'ts-jest',
|
'^.+\\.ts$': 'ts-jest',
|
||||||
},
|
},
|
||||||
roots: ['<rootDir>/src'],
|
roots: ['<rootDir>/tests'],
|
||||||
testMatch: ['**/*.test.ts'],
|
testMatch: ['**/*.test.ts'],
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,9 +30,9 @@
|
||||||
"prepack": "yarn build",
|
"prepack": "yarn build",
|
||||||
"prebuild": "rm -rf dist",
|
"prebuild": "rm -rf dist",
|
||||||
"build": "tsc --project .",
|
"build": "tsc --project .",
|
||||||
"lint": "eslint src/**/*.ts",
|
"lint": "eslint tests/**/*.ts src/**/*.ts",
|
||||||
"lint:fix": "yarn lint --fix",
|
"lint:fix": "yarn lint --fix",
|
||||||
"test": "jest src",
|
"test": "jest tests",
|
||||||
"test:watch": "yarn test --watchAll",
|
"test:watch": "yarn test --watchAll",
|
||||||
"test:coverage": "yarn test --coverage",
|
"test:coverage": "yarn test --coverage",
|
||||||
"types": "tsc --noEmit src/**/*.ts",
|
"types": "tsc --noEmit src/**/*.ts",
|
||||||
|
|
|
@ -7,7 +7,7 @@ import {
|
||||||
getPreviousPackageStats,
|
getPreviousPackageStats,
|
||||||
mergeDefaultArguments,
|
mergeDefaultArguments,
|
||||||
} from './utils'
|
} from './utils'
|
||||||
import type { PackwatchArguments } from './index.d'
|
import type { PackwatchArguments } from './types'
|
||||||
import { assertInPackageRoot } from './invariants'
|
import { assertInPackageRoot } from './invariants'
|
||||||
import logger from './logger'
|
import logger from './logger'
|
||||||
|
|
||||||
|
@ -52,8 +52,7 @@ export default async function packwatch(
|
||||||
limit,
|
limit,
|
||||||
limitBytes,
|
limitBytes,
|
||||||
} = previousStats
|
} = previousStats
|
||||||
const hasExceededLimit =
|
const hasExceededLimit = limitBytes && packageSizeBytes > limitBytes
|
||||||
packageSizeBytes > (limitBytes ?? Number.MAX_SAFE_INTEGER)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we are updating the manifest, we can write right away and terminate.
|
* If we are updating the manifest, we can write right away and terminate.
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { spawnSync } from 'child_process'
|
||||||
import { readFileSync, writeFileSync } from 'fs'
|
import { readFileSync, writeFileSync } from 'fs'
|
||||||
import { join, resolve } from 'path'
|
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 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})/
|
const UNPACKED_SIZE_PATT = /unpacked size:\s*([0-9]+\.?[0-9]*\s+[A-Za-z]{1,2})/
|
||||||
|
|
|
@ -2,11 +2,14 @@ import { promises as fs } from 'fs'
|
||||||
import { tmpdir } from 'os'
|
import { tmpdir } from 'os'
|
||||||
import { join, resolve } from 'path'
|
import { join, resolve } from 'path'
|
||||||
|
|
||||||
import type { Report } from '../index.d'
|
import type { Report } from '../src/types'
|
||||||
import packwatch from '..'
|
import packwatch from '../src'
|
||||||
|
|
||||||
|
let workspace: string | null
|
||||||
|
|
||||||
async function prepareWorkspace(): Promise<string> {
|
async function prepareWorkspace(): Promise<string> {
|
||||||
const workspacePath = await fs.mkdtemp(`${tmpdir()}/`)
|
const workspacePath = await fs.mkdtemp(`${tmpdir()}/`)
|
||||||
|
workspace = workspacePath
|
||||||
return workspacePath
|
return workspacePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,28 +38,21 @@ async function createManifest(
|
||||||
const path = resolve(join(cwd, '.packwatch.json'))
|
const path = resolve(join(cwd, '.packwatch.json'))
|
||||||
await createFile(path, JSON.stringify(configuration))
|
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 () => {
|
afterEach(async () => {
|
||||||
jest.restoreAllMocks()
|
jest.restoreAllMocks()
|
||||||
|
|
||||||
if (workspacePath) {
|
if (workspace) {
|
||||||
await cleanUpWorkspace([workspacePath])
|
await cleanUpWorkspace([workspace])
|
||||||
workspacePath = null
|
workspace = null
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it('warns the user and errors if run away from package.json', async () => {
|
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 () =>
|
await expect(async () =>
|
||||||
packwatch({ cwd: workspacePath }),
|
packwatch({ cwd: workspacePath }),
|
||||||
).rejects.toThrow('NOT_IN_PACKAGE_ROOT')
|
).rejects.toThrow('NOT_IN_PACKAGE_ROOT')
|
||||||
|
@ -71,7 +67,7 @@ describe('Packwatch', () => {
|
||||||
|
|
||||||
describe('without manifest', () => {
|
describe('without manifest', () => {
|
||||||
it('generates the initial manifest properly', async () => {
|
it('generates the initial manifest properly', async () => {
|
||||||
workspacePath = await prepareWorkspace()
|
const workspacePath = await prepareWorkspace()
|
||||||
await createPackageJson(workspacePath)
|
await createPackageJson(workspacePath)
|
||||||
|
|
||||||
await expect(async () =>
|
await expect(async () =>
|
||||||
|
@ -89,7 +85,9 @@ describe('Packwatch', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('outputs expected messaging', async () => {
|
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 createPackageJson(workspacePath)
|
||||||
|
|
||||||
await expect(async () =>
|
await expect(async () =>
|
||||||
|
@ -111,7 +109,8 @@ describe('Packwatch', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('outputs expected messaging when not updating the manifest', async () => {
|
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)
|
await createPackageJson(workspacePath)
|
||||||
|
|
||||||
|
@ -128,13 +127,15 @@ describe('Packwatch', () => {
|
||||||
|
|
||||||
describe('with manifest', () => {
|
describe('with manifest', () => {
|
||||||
it('messages when the size is equal to the limit', async () => {
|
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 createPackageJson(workspacePath)
|
||||||
await createManifest(workspacePath, {
|
await createManifest(workspacePath, {
|
||||||
limit: '160B',
|
limit: '160B',
|
||||||
packageSize: '160B',
|
packageSize: '160B',
|
||||||
|
packageSizeBytes: 160,
|
||||||
unpackedSize: '150B',
|
unpackedSize: '150B',
|
||||||
|
unpackedSizeBytes: 150,
|
||||||
})
|
})
|
||||||
await packwatch({ cwd: workspacePath })
|
await packwatch({ cwd: workspacePath })
|
||||||
expect(mockLogger.mock.calls).toHaveLength(1)
|
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 () => {
|
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 createPackageJson(workspacePath)
|
||||||
await createManifest(workspacePath, {
|
await createManifest(workspacePath, {
|
||||||
limit: '170B',
|
limit: '170B',
|
||||||
packageSize: '160B',
|
packageSize: '160B',
|
||||||
|
packageSizeBytes: 160,
|
||||||
unpackedSize: '150B',
|
unpackedSize: '150B',
|
||||||
|
unpackedSizeBytes: 150,
|
||||||
})
|
})
|
||||||
|
|
||||||
await packwatch({ cwd: workspacePath })
|
await packwatch({ cwd: workspacePath })
|
||||||
|
@ -164,13 +167,15 @@ describe('Packwatch', () => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
it('messages when the size is lower than the limit (growth)', async () => {
|
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 createPackageJson(workspacePath)
|
||||||
await createManifest(workspacePath, {
|
await createManifest(workspacePath, {
|
||||||
limit: '180B',
|
limit: '180B',
|
||||||
packageSize: '150B',
|
packageSize: '150B',
|
||||||
|
packageSizeBytes: 150,
|
||||||
unpackedSize: '140B',
|
unpackedSize: '140B',
|
||||||
|
unpackedSizeBytes: 140,
|
||||||
})
|
})
|
||||||
|
|
||||||
await packwatch({ cwd: workspacePath })
|
await packwatch({ cwd: workspacePath })
|
||||||
|
@ -182,13 +187,15 @@ describe('Packwatch', () => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
it('messages when the size is lower than the limit (shrinkage)', async () => {
|
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 createPackageJson(workspacePath)
|
||||||
await createManifest(workspacePath, {
|
await createManifest(workspacePath, {
|
||||||
limit: '180B',
|
limit: '180B',
|
||||||
packageSize: '170B',
|
packageSize: '170B',
|
||||||
|
packageSizeBytes: 170,
|
||||||
unpackedSize: '140B',
|
unpackedSize: '140B',
|
||||||
|
unpackedSizeBytes: 140,
|
||||||
})
|
})
|
||||||
|
|
||||||
await packwatch({ cwd: workspacePath })
|
await packwatch({ cwd: workspacePath })
|
||||||
|
@ -200,13 +207,15 @@ describe('Packwatch', () => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
it('messages when the size exceeds the limit', async () => {
|
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 createPackageJson(workspacePath)
|
||||||
await createManifest(workspacePath, {
|
await createManifest(workspacePath, {
|
||||||
limit: '10B',
|
limit: '10B',
|
||||||
packageSize: '170B',
|
packageSize: '170B',
|
||||||
|
packageSizeBytes: 170,
|
||||||
unpackedSize: '140B',
|
unpackedSize: '140B',
|
||||||
|
unpackedSizeBytes: 140,
|
||||||
})
|
})
|
||||||
|
|
||||||
await expect(async () =>
|
await expect(async () =>
|
||||||
|
@ -221,13 +230,15 @@ describe('Packwatch', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('messages when updating the manifest', async () => {
|
it('messages when updating the manifest', async () => {
|
||||||
workspacePath = await prepareWorkspace()
|
const workspacePath = await prepareWorkspace()
|
||||||
|
const mockLogger = jest.spyOn(console, 'log')
|
||||||
await createPackageJson(workspacePath)
|
await createPackageJson(workspacePath)
|
||||||
await createManifest(workspacePath, {
|
await createManifest(workspacePath, {
|
||||||
limit: '10B',
|
limit: '10B',
|
||||||
packageSize: '170B',
|
packageSize: '170B',
|
||||||
|
packageSizeBytes: 170,
|
||||||
unpackedSize: '140B',
|
unpackedSize: '140B',
|
||||||
|
unpackedSizeBytes: 140,
|
||||||
})
|
})
|
||||||
|
|
||||||
await packwatch({ cwd: workspacePath, isUpdatingManifest: true })
|
await packwatch({ cwd: workspacePath, isUpdatingManifest: true })
|
|
@ -1,4 +1,4 @@
|
||||||
import { convertSizeToBytes } from '../utils'
|
import { convertSizeToBytes } from '../src/utils'
|
||||||
|
|
||||||
describe('utils', () => {
|
describe('utils', () => {
|
||||||
it.each`
|
it.each`
|
|
@ -69,5 +69,5 @@
|
||||||
"skipLibCheck": true, /* Skip type checking of declaration files. */
|
"skipLibCheck": true, /* Skip type checking of declaration files. */
|
||||||
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
||||||
},
|
},
|
||||||
"exclude": ["dist/**/*", "src/__tests__/**/*"]
|
"exclude": ["dist/**/*", "tests/**/*"]
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue