From c24382a1d725103d8bc3615b6c1f8630b3efcbc9 Mon Sep 17 00:00:00 2001 From: Bart Mesuere Date: Fri, 21 Jun 2024 14:30:08 +0200 Subject: [PATCH] add tests --- lib/commands/prot2pept.ts | 9 ++- tests/commands/prot2pept.test.ts | 124 +++++++++++++++++++++++++++++++ tsconfig.json | 1 + 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 tests/commands/prot2pept.test.ts diff --git a/lib/commands/prot2pept.ts b/lib/commands/prot2pept.ts index ac7c8fb7..3b3a6b01 100644 --- a/lib/commands/prot2pept.ts +++ b/lib/commands/prot2pept.ts @@ -23,7 +23,14 @@ The input should have either one protein sequence per line or contain a FASTA fo */ async run() { this.parseArguments(); - const pattern = new RegExp(this.program.opts().pattern, "g"); + + let pattern; + try { + pattern = new RegExp(this.program.opts().pattern, "g"); + } catch (e) { + this.program.error(`Your pattern was invalid: ${(e as Error).message}`); + //process.exit(1); + } let fasta = false; let protein = []; diff --git a/tests/commands/prot2pept.test.ts b/tests/commands/prot2pept.test.ts new file mode 100644 index 00000000..ca01533a --- /dev/null +++ b/tests/commands/prot2pept.test.ts @@ -0,0 +1,124 @@ +import { Prot2pept } from '../../lib/commands/prot2pept'; +import { jest } from '@jest/globals'; +import * as mock from 'mock-stdin'; + +let output: string[]; +let error: string[]; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const writeSpy = jest + .spyOn(process.stdout, "write") + .mockImplementation((data: unknown) => { output.push(data as string); return true; }); +const errorSpy = jest + .spyOn(process.stderr, "write") + .mockImplementation((data: unknown) => { error.push(data as string); return true; }); + +beforeEach(() => { + output = []; + error = []; +}); + +test('test single line input 1', async () => { + const stdin = mock.stdin(); + + const command = new Prot2pept(); + const run = command.run(); + + stdin.send("AALTERAALTERPAALTER\n"); + stdin.end(); + + await run; + + expect(errorSpy).toHaveBeenCalledTimes(0); + expect(output.join("").trimEnd()).toBe("AALTER\nAALTERPAALTER"); +}); + +test('test single line input 2', async () => { + const stdin = mock.stdin(); + + const command = new Prot2pept(); + const run = command.run(); + + stdin.send("KRKPR\n"); + stdin.end(); + + await run; + + expect(errorSpy).toHaveBeenCalledTimes(0); + expect(output.join("").trimEnd()).toBe("K\nR\nKPR"); +}); + +test('test multi line input', async () => { + const stdin = mock.stdin(); + + const command = new Prot2pept(); + const run = command.run(); + + stdin.send("AALTERAALTERPAALTER\n"); + stdin.send("AALTERAA\n"); + stdin.end(); + + await run; + + expect(errorSpy).toHaveBeenCalledTimes(0); + expect(output.join("").trimEnd()).toBe("AALTER\nAALTERPAALTER\nAALTER\nAA"); +}); + +test('test fasta input 1', async () => { + const stdin = mock.stdin(); + + const command = new Prot2pept(); + const run = command.run(); + + stdin.send(">AKA\nAALTERAALTERPAALTER\n"); + stdin.end(); + + await run; + + expect(errorSpy).toHaveBeenCalledTimes(0); + expect(output.join("").trimEnd()).toBe(">AKA\nAALTER\nAALTERPAALTER"); +}); + +test('test fasta input 2', async () => { + const stdin = mock.stdin(); + + const command = new Prot2pept(); + const run = command.run(); + + stdin.send(">AKA\nAAL\nT\nERAALTER\nP\nAALTER\n"); + stdin.end(); + + await run; + + expect(errorSpy).toHaveBeenCalledTimes(0); + expect(output.join("").trimEnd()).toBe(">AKA\nAALTER\nAALTERPAALTER"); +}); + +test('test fasta input 3', async () => { + const stdin = mock.stdin(); + + const command = new Prot2pept(); + const run = command.run(); + + stdin.send(">AKA\nAAL\nT\n>\nERAALTER\nP\nAALTER"); + stdin.end(); + + await run; + + expect(errorSpy).toHaveBeenCalledTimes(0); + expect(output.join("").trimEnd()).toBe(">AKA\nAALT\n>\nER\nAALTERPAALTER"); +}); + +test('test custom pattern', async () => { + const stdin = mock.stdin(); + + const command = new Prot2pept({ args: ["--pattern", "([KR])([^A])"] }); + const run = command.run(); + + stdin.send("AALTERAALTERPAALTER\n"); + stdin.end(); + + await run; + + expect(errorSpy).toHaveBeenCalledTimes(0); + expect(output.join("").trimEnd()).toBe("AALTERAALTER\nPAALTER"); +}); diff --git a/tsconfig.json b/tsconfig.json index 5c9bef88..23047235 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,5 +8,6 @@ "module": "NodeNext", "strict": true, "resolveJsonModule": true, + "target": "esnext" } }