From 3432f3d813da959ca9bc88e9f0447a1f71105fc7 Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Thu, 27 Jul 2023 08:10:28 +0000 Subject: [PATCH 1/5] cli: add simple integration tests --- test/integration/bin/cli.js | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/integration/bin/cli.js diff --git a/test/integration/bin/cli.js b/test/integration/bin/cli.js new file mode 100644 index 000000000..76de38d6d --- /dev/null +++ b/test/integration/bin/cli.js @@ -0,0 +1,41 @@ +const { execSync } = require('node:child_process'); +const uuid = require('uuid').v4; + +describe('cli', () => { + it('should return status code 1 if no command is issued', () => { + let thrown = false; // pattern from test/unit/util/crypto.js + try { + // eslint-disable-next-line no-use-before-define + cli('--email example@example.com'); + } catch (err) { + err.status.should.equal(1); + thrown = true; + } + thrown.should.equal(true); + }); + + describe('command: user-create', () => { + it('should return status code 0 and user details on success', () => { + // eslint-disable-next-line no-use-before-define + const email = randomEmail(); + + // eslint-disable-next-line no-use-before-define + const [, , js] = cli(`--email ${email} user-create`, { input: 'strong-password-101' }) + .match(/^([^{]*)(.*)$/ms); + + js.should.match(new RegExp(`email: '${email}',`)); + js.should.not.match(/password/); + }); + }); +}); + +function cli(argString, opts) { + return execSync( + 'node lib/bin/cli.js ' + argString, + { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'], ...opts }, + ); +} + +function randomEmail() { + return uuid() + '@example.com'; +} From ce8fa271ad1b0741308429a9cf31de18d8d8a8df Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Wed, 6 Sep 2023 09:02:54 +0000 Subject: [PATCH 2/5] expand tests, improve output on fail --- test/integration/bin/cli.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/integration/bin/cli.js b/test/integration/bin/cli.js index 76de38d6d..9ccb91964 100644 --- a/test/integration/bin/cli.js +++ b/test/integration/bin/cli.js @@ -6,7 +6,19 @@ describe('cli', () => { let thrown = false; // pattern from test/unit/util/crypto.js try { // eslint-disable-next-line no-use-before-define - cli('--email example@example.com'); + cli('', { stdio: ['pipe', 'pipe', 'ignore'] }); + } catch (err) { + err.status.should.equal(1); + thrown = true; + } + thrown.should.equal(true); + }); + + it('should return status code 1 if non-existent command is issued', () => { + let thrown = false; // pattern from test/unit/util/crypto.js + try { + // eslint-disable-next-line no-use-before-define + cli('user-congratulate', { stdio: ['pipe', 'pipe', 'ignore'] }); } catch (err) { err.status.should.equal(1); thrown = true; @@ -32,7 +44,7 @@ describe('cli', () => { function cli(argString, opts) { return execSync( 'node lib/bin/cli.js ' + argString, - { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'], ...opts }, + { encoding: 'utf-8', ...opts }, ); } From d30b9d708302bd307c57eea53d22c8e891a639c1 Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Wed, 6 Sep 2023 09:19:29 +0000 Subject: [PATCH 3/5] try test-cntainer --- test/integration/bin/cli.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/integration/bin/cli.js b/test/integration/bin/cli.js index 9ccb91964..c2d0cec41 100644 --- a/test/integration/bin/cli.js +++ b/test/integration/bin/cli.js @@ -1,8 +1,10 @@ const { execSync } = require('node:child_process'); const uuid = require('uuid').v4; -describe('cli', () => { - it('should return status code 1 if no command is issued', () => { +const { testContainer } = require('../setup'); + +describe.only('cli', () => { + it('should return status code 1 if no command is issued', testContainer(async () => { let thrown = false; // pattern from test/unit/util/crypto.js try { // eslint-disable-next-line no-use-before-define @@ -12,9 +14,9 @@ describe('cli', () => { thrown = true; } thrown.should.equal(true); - }); + })); - it('should return status code 1 if non-existent command is issued', () => { + it('should return status code 1 if non-existent command is issued', testContainer(async () => { let thrown = false; // pattern from test/unit/util/crypto.js try { // eslint-disable-next-line no-use-before-define @@ -24,10 +26,10 @@ describe('cli', () => { thrown = true; } thrown.should.equal(true); - }); + })); describe('command: user-create', () => { - it('should return status code 0 and user details on success', () => { + it('should return status code 0 and user details on success', testContainer(async () => { // eslint-disable-next-line no-use-before-define const email = randomEmail(); @@ -37,7 +39,7 @@ describe('cli', () => { js.should.match(new RegExp(`email: '${email}',`)); js.should.not.match(/password/); - }); + })); }); }); From 02fa0d9173268505e79bc88307a98cb7278eeffd Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Wed, 6 Sep 2023 09:19:48 +0000 Subject: [PATCH 4/5] wip --- test/integration/bin/cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/bin/cli.js b/test/integration/bin/cli.js index c2d0cec41..48e0e4853 100644 --- a/test/integration/bin/cli.js +++ b/test/integration/bin/cli.js @@ -3,7 +3,7 @@ const uuid = require('uuid').v4; const { testContainer } = require('../setup'); -describe.only('cli', () => { +describe('cli', () => { it('should return status code 1 if no command is issued', testContainer(async () => { let thrown = false; // pattern from test/unit/util/crypto.js try { From 1701bd34be3f54b39379821afa5b3885bc1621d2 Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Wed, 6 Sep 2023 10:01:13 +0000 Subject: [PATCH 5/5] revert testContainer() --- test/integration/bin/cli.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/test/integration/bin/cli.js b/test/integration/bin/cli.js index 48e0e4853..9ccb91964 100644 --- a/test/integration/bin/cli.js +++ b/test/integration/bin/cli.js @@ -1,10 +1,8 @@ const { execSync } = require('node:child_process'); const uuid = require('uuid').v4; -const { testContainer } = require('../setup'); - describe('cli', () => { - it('should return status code 1 if no command is issued', testContainer(async () => { + it('should return status code 1 if no command is issued', () => { let thrown = false; // pattern from test/unit/util/crypto.js try { // eslint-disable-next-line no-use-before-define @@ -14,9 +12,9 @@ describe('cli', () => { thrown = true; } thrown.should.equal(true); - })); + }); - it('should return status code 1 if non-existent command is issued', testContainer(async () => { + it('should return status code 1 if non-existent command is issued', () => { let thrown = false; // pattern from test/unit/util/crypto.js try { // eslint-disable-next-line no-use-before-define @@ -26,10 +24,10 @@ describe('cli', () => { thrown = true; } thrown.should.equal(true); - })); + }); describe('command: user-create', () => { - it('should return status code 0 and user details on success', testContainer(async () => { + it('should return status code 0 and user details on success', () => { // eslint-disable-next-line no-use-before-define const email = randomEmail(); @@ -39,7 +37,7 @@ describe('cli', () => { js.should.match(new RegExp(`email: '${email}',`)); js.should.not.match(/password/); - })); + }); }); });