Skip to content

Commit 542db18

Browse files
committed
test(data-masking): add type tests for generics
1 parent a76a17f commit 542db18

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "../../",
5+
"noEmit": true
6+
},
7+
"include": ["../src/**/*", "./**/*"]
8+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expectTypeOf, it } from 'vitest';
2+
import { DataMasking } from '../../src/index.js';
3+
4+
describe('DataMasking type tests', () => {
5+
const masker = new DataMasking();
6+
7+
it('erase preserves the input type', () => {
8+
const data = { name: 'Jane', ssn: '123' };
9+
const result = masker.erase(data, { fields: ['ssn'] });
10+
11+
expectTypeOf(result).toEqualTypeOf<{ name: string; ssn: string }>();
12+
});
13+
14+
it('erase returns the same type for arrays', () => {
15+
const data = [1, 2, 3];
16+
const result = masker.erase(data);
17+
18+
expectTypeOf(result).toEqualTypeOf<number[]>();
19+
});
20+
21+
it('erase returns null for null input', () => {
22+
const result = masker.erase(null);
23+
24+
expectTypeOf(result).toEqualTypeOf<null>();
25+
});
26+
27+
it('encrypt returns T | string', async () => {
28+
const data = { secret: 'value' };
29+
const result = await masker.encrypt(data);
30+
31+
expectTypeOf(result).toEqualTypeOf<{ secret: string } | string>();
32+
});
33+
34+
it('decrypt infers T from the generic parameter', async () => {
35+
const result = await masker.decrypt<{ secret: string }>('ciphertext');
36+
37+
expectTypeOf(result).toEqualTypeOf<{ secret: string }>();
38+
});
39+
40+
it('decrypt infers T from object input', async () => {
41+
const data = { ssn: 'encrypted' };
42+
const result = await masker.decrypt(data, { fields: ['ssn'] });
43+
44+
expectTypeOf(result).toEqualTypeOf<{ ssn: string }>();
45+
});
46+
});

packages/data-masking/vitest.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { defineProject } from 'vitest/config';
33
export default defineProject({
44
test: {
55
environment: 'node',
6-
setupFiles: ['../testing/src/setupEnv.ts'],
6+
typecheck: {
7+
tsconfig: './tests/tsconfig.json',
8+
include: ['./tests/types/**/*.ts'],
9+
},
710
},
811
});

0 commit comments

Comments
 (0)