Skip to content

Commit

Permalink
feat(uuid): expose genTestUuid procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed Aug 3, 2024
1 parent 25b80fb commit 2c17d2c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
22 changes: 21 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@trivago/prettier-plugin-sort-imports": "2.0.4",
"@tsconfig/node-lts-strictest": "18.12.1",
"@types/jest": "29.2.4",
"@types/uuid": "10.0.0",
"@typescript-eslint/eslint-plugin": "5.46.1",
"@typescript-eslint/parser": "5.46.1",
"core-js": "3.26.1",
Expand All @@ -80,6 +81,7 @@
},
"license": "MIT",
"dependencies": {
"@ehmpathy/error-fns": "1.3.1"
"@ehmpathy/error-fns": "1.3.1",
"uuid": "10.0.0"
}
}
18 changes: 18 additions & 0 deletions src/genTestUuid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { genTestUuid, isTestUuid } from './genTestUuid';

describe('genTestUuid', () => {
it('should generate a uuid', () => {
const uuid = genTestUuid();
expect(uuid).toBeTruthy();
});
it('should generate a uuid that is namespaced for testing by whodis', () => {
const uuid = genTestUuid();
expect(uuid).toMatch(/^beef.*-.*-.*-.*-.*beef$/);
expect(isTestUuid(uuid)).toEqual(true);
});
it('should generate a uuid that is different every time', () => {
const uuid1 = genTestUuid();
const uuid2 = genTestUuid();
expect(uuid1).not.toEqual(uuid2);
});
});
17 changes: 17 additions & 0 deletions src/genTestUuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { v4 as uuid } from 'uuid';

/**
* generates a random uuid namespaced for tests, pattern `beef****-****-****-****-********beef`
*
* usecase
* - produces a uuid that can be clearly identified as one produced for tests
*/
export const genTestUuid = (): string => {
return ['beef', uuid().slice(4, -4), 'beef'].join('');
};

/**
* decides whether a uuid is namespaced for tests
*/
export const isTestUuid = (userUuid: string): boolean =>
/^beef.{4}-.{4}-.{4}-.{4}-.{8}beef$/.test(userUuid);
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { given, when, then } from './givenWhenThen';
export { genTestUuid } from './genTestUuid';

// forward the getError method since it is almost always needed with tests
export { getError } from '@ehmpathy/error-fns';

0 comments on commit 2c17d2c

Please sign in to comment.