Skip to content

Commit

Permalink
bugfix: rebuild project
Browse files Browse the repository at this point in the history
  • Loading branch information
victorperin committed Jan 11, 2024
1 parent 17c8abc commit d34bc2a
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 63 deletions.
9 changes: 7 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ module.exports = {
testMatch: ['<rootDir>/tests/**/*.test.ts', '<rootDir>/src/**/*.test.ts'],
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/coverage/', '<rootDir>/.nyc_output/'],
verbose: true,
globals: {
'ts-jest': { compiler: 'ttypescript' },
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
compiler: 'ts-patch/compiler',
},
],
},
}
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
"publish:docs": "gh-pages -d docs-web",
"build": "tsc",
"semantic-release": "semantic-release",
"prepare": "husky install"
"prepare": "husky install && ts-patch install -s"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@jimp/core": "^0.22.10",
"boxen": "^5.1.1",
"chalk": "^4.1.2",
"clipboardy": "^2.3.0",
"common-tags": "^1.8.0",
"jimp": "^0.16.1",
"jimp": "^0.16.13",
"open": "^8.2.1",
"qrcode-reader": "^1.0.4",
"yargs": "^17.1.1"
Expand All @@ -45,8 +46,11 @@
"@stryker-mutator/typescript-checker": "^5.4.0",
"@types/common-tags": "^1.8.1",
"@types/jest": "^27.0.1",
"@types/node": "^20.11.0",
"@types/yargs": "^17.0.32",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"core": "link:@types/@jimp/core",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-editorconfig": "^3.0.2",
Expand All @@ -55,23 +59,23 @@
"execa": "^5.1.1",
"gh-pages": "^3.2.3",
"husky": "^7.0.2",
"jest": "^27.2.0",
"jest": "^29.7.0",
"jest-extended": "^0.11.5",
"jest-ts-auto-mock": "^2.0.0",
"jest-ts-auto-mock": "^2.1.0",
"lint-staged": "^11.1.2",
"nyc": "^15.1.0",
"prettier": "^2.4.0",
"semantic-release": "^19.0.3",
"source-map-support": "^0.5.20",
"ts-auto-mock": "^3.5.0",
"ts-jest": "^27.0.5",
"ts-auto-mock": "^3.7.2",
"ts-jest": "^29.1.1",
"ts-node": "^10.2.1",
"ts-patch": "^3.1.2",
"tsc-prog": "^2.2.1",
"ttypescript": "^1.5.12",
"typedoc": "^0.22.3",
"typedoc-neo-theme": "^1.1.1",
"typedoc-plugin-markdown": "^3.11.0",
"typescript": "4.4"
"typescript": "~5.3.3"
},
"repository": {
"type": "git",
Expand Down
39 changes: 13 additions & 26 deletions src/cli/execution.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { mocked } from 'ts-jest/utils'
import { createMock } from 'ts-auto-mock'

import yargs, { Arguments } from 'yargs'
import { greenBox } from '../infrastructure/boxen'
import { scanFromFileOnCli } from '../pipelines/scanFromFile'
import execution from './execution'

const scanFromFileMocked = mocked(scanFromFileOnCli, true)
const scanFromFileMocked = jest.mocked(scanFromFileOnCli)

const yargsMocked = mocked(yargs, true)
const yargsMocked = jest.mocked(yargs, true)
yargsMocked.mockReturnValue(yargsMocked)
yargsMocked.strict.mockReturnValue(yargsMocked)
yargsMocked.example.mockReturnValue(yargsMocked)
Expand All @@ -18,7 +17,7 @@ yargsMocked.command.mockReturnValue(yargsMocked)
yargsMocked.positional.mockReturnValue(yargsMocked)
const defaultArgv = createMock<Arguments>({})

const greenBoxMocked = mocked(greenBox)
const greenBoxMocked = jest.mocked(greenBox)

jest.mock('../pipelines/scanFromFile', () => ({ scanFromFileOnCli: jest.fn() }))
jest.mock('yargs', yargsMocked)
Expand Down Expand Up @@ -59,32 +58,20 @@ test('should catch if scanFromFile fails', async () => {
expect(console.error).toBeCalledWith(expectedError.message)
})

test('should warn if no argument passed', async () => {
scanFromFileMocked.mockResolvedValue()
yargsMocked.mockImplementationOnce(() => {
throw new Error('teste')
})

const expectedExecution = () => execution([])

expect(expectedExecution).toThrow()
expect(scanFromFileMocked).not.toBeCalled()
})

test('should provide command and positional', async () => {
await execution([])

expect(greenBoxMocked).toBeCalled()
expect(yargsMocked.command.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"$0 <file>",
"Scan a QR Code from a file",
]
`)
expect(yargsMocked.positional.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"file",
Object {
{
"demandOption": true,
"describe": "Path to the file to scan",
"type": "string",
Expand All @@ -98,33 +85,33 @@ test('should provide examples and options', async () => {

expect(greenBoxMocked).toBeCalled()
expect(yargsMocked.example.mock.calls[0][0]).toMatchInlineSnapshot(`
Array [
Array [
[
[
"qrscanner ./qrCode.jpg",
"This message is written in a QR Code",
],
Array [
[
"qrscanner ./qrCode.jpg --clear",
"
This message is written in a QR Code",
],
]
`)
expect(yargsMocked.options.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"clear": Object {
{
"clear": {
"alias": "c",
"boolean": true,
"default": false,
"description": "Clear output, just print the QR Code scan result",
},
"clipboard": Object {
"clipboard": {
"alias": "p",
"boolean": true,
"default": false,
"description": "Copy the qr code value to your clipboard",
},
"open": Object {
"open": {
"alias": "o",
"boolean": true,
"default": false,
Expand Down
4 changes: 2 additions & 2 deletions src/cli/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { scanFromFileOnCli } from '../pipelines/scanFromFile'
import { greenBox } from '../infrastructure/boxen'
import flags from './flags'

const execution = (args: string[]): Promise<void> | void => {
const execution = async (args: string[]): Promise<void> => {
const yargsInstance = yargs(args)
.strict()
.example([
Expand All @@ -21,7 +21,7 @@ const execution = (args: string[]): Promise<void> | void => {
.options(flags)
.help()

const argv = yargsInstance.argv
const argv = await yargsInstance.argv

const { _, $0, file, ...flagsTreated } = argv

Expand Down
3 changes: 1 addition & 2 deletions src/handlers/flags.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { mocked } from 'ts-jest/utils'
import open from 'open'
import { doOpen } from './flags'
import { createMock } from 'ts-auto-mock'
import { Flags } from '../cli/flags'

const openMocked = mocked(open, true)
const openMocked = jest.mocked(open, true)
jest.mock('open')
beforeEach(openMocked.mockReset)

Expand Down
3 changes: 1 addition & 2 deletions src/infrastructure/boxen.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { mocked } from 'ts-jest/utils'
import boxen from 'boxen'
import { greenBox } from './boxen'

const boxenMocked = mocked(boxen, true)
const boxenMocked = jest.mocked(boxen)

jest.mock('boxen')
beforeEach(() => boxenMocked.mockReset())
Expand Down
3 changes: 1 addition & 2 deletions src/infrastructure/jimp.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { mocked } from 'ts-jest/utils'
import { createMock } from 'ts-auto-mock'
import Jimp from 'jimp'
import { Bitmap } from '@jimp/core'
import { getBitmap } from './jimp'

jest.mock('jimp', () => ({ read: jest.fn() }))
const jimpMock = mocked(Jimp)
const jimpMock = jest.mocked(Jimp)
beforeEach(jimpMock.read.mockReset)

describe('jimp/getBitmap', () => {
Expand Down
3 changes: 1 addition & 2 deletions src/infrastructure/qrcode-reader.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import qrcodeReaderLib from 'qrcode-reader'
import qrcodeReader from './qrcode-reader'
import { createMock } from 'ts-auto-mock'
import { mocked } from 'ts-jest/utils'
import { Bitmap } from '@jimp/core'

jest.mock('qrcode-reader')
const qrcodeReaderLibMocked = mocked(qrcodeReaderLib, true)
const qrcodeReaderLibMocked = jest.mocked(qrcodeReaderLib, true)

beforeEach(qrcodeReaderLibMocked.mockClear)

Expand Down
12 changes: 5 additions & 7 deletions src/pipelines/scanFromBitmap.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import 'jest-extended'
import { scanFromBitmap } from './scanFromBitmap'
import readQR from '../infrastructure/qrcode-reader'
import { mocked } from 'ts-jest/utils'
import { Bitmap } from '@jimp/core'
import { createMock } from 'ts-auto-mock'
import { doFlagClipboard, doOpen, FlagFunction } from '../handlers/flags'
import errorHandlers from '../handlers/error'
import { MockedFunction } from 'ts-jest/dist/utils/testing'

jest.mock('../infrastructure/qrcode-reader')
jest.mock('../handlers/flags')
jest.mock('../handlers/error')
const readQRMock = mocked(readQR)
const doFlagClipboardMock = mocked(doFlagClipboard)
const doOpenMock = mocked(doOpen)
const errorHandlerMock = mocked(errorHandlers.scanFromBitmap)
const readQRMock = jest.mocked(readQR)
const doFlagClipboardMock = jest.mocked(doFlagClipboard)
const doOpenMock = jest.mocked(doOpen)
const errorHandlerMock = jest.mocked(errorHandlers.scanFromBitmap)

readQRMock.mockResolvedValue('readQR fake result')
doFlagClipboardMock.mockImplementation(() => jest.fn((result) => result))
Expand All @@ -31,7 +29,7 @@ describe('scanFromBitmap', () => {
})

describe('flags tests', () => {
type FlagMockedFunction = MockedFunction<FlagFunction>
type FlagMockedFunction = jest.MockedFn<FlagFunction>
type FlagsTestTuple = [string, FlagMockedFunction]

const flags: FlagsTestTuple[] = [
Expand Down
13 changes: 6 additions & 7 deletions src/pipelines/scanFromFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { mocked } from 'ts-jest/utils'
import * as jimp from '../infrastructure/jimp'
import clipboardy from 'clipboardy'
import open from 'open'
Expand All @@ -11,12 +10,12 @@ import { Bitmap } from '@jimp/core'
import { scanFromFileOnCli, scanFromFile } from './scanFromFile'
import { Flags } from '../cli/flags'

const JimpMocked = mocked(jimp, true)
const clipboardyMocked = mocked(clipboardy, true)
const openMocked = mocked(open, true)
const qrReaderMocked = mocked(qrReader, true)
const boxenMocked = mocked(boxen, true)
const errorHandlerMocked = mocked(errorHandler, true)
const JimpMocked = jest.mocked(jimp, true)
const clipboardyMocked = jest.mocked(clipboardy, true)
const openMocked = jest.mocked(open, true)
const qrReaderMocked = jest.mocked(qrReader, true)
const boxenMocked = jest.mocked(boxen, true)
const errorHandlerMocked = jest.mocked(errorHandler, true)

jest.mock('clipboardy')
jest.mock('open')
Expand Down
10 changes: 8 additions & 2 deletions tests/integration/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ const ERROR = {

const CLI_PATH = './qrscanner'

const envVars = { LANG: 'en_US' }

/*
using execa with nyc is a workarround to get coverage from jest.
More on: https://github.com/facebook/jest/issues/3190#issuecomment-354758036
*/
const execute = (args: string[] = []): execa.ExecaChildProcess =>
process.env.STRYKER_TEST
? execa('./node_modules/.bin/nyc', ['--silent', '--no-clean', 'node', CLI_PATH, ...args])
: execa('./node_modules/.bin/nyc', ['--silent', '--no-clean', CLI_PATH, ...args])
? execa('./node_modules/.bin/nyc', ['--silent', '--no-clean', 'node', CLI_PATH, ...args], {
env: envVars,
})
: execa('./node_modules/.bin/nyc', ['--silent', '--no-clean', CLI_PATH, ...args], {
env: envVars,
})

beforeAll(() => jest.setTimeout(300000))

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"rootDirs": [
Expand Down

0 comments on commit d34bc2a

Please sign in to comment.