From 5995a50f29027bc4f235253f227f299d68d5c428 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Mon, 28 Jul 2025 23:35:40 -0700 Subject: [PATCH] Add tests for `instanceof` behavior In preparation for possible future refactoring for these classes to return a `Proxy` object (this pattern is required to simulate dynamic superclasses, e.g., `CSVReader` returning either `ReadableStream` OR `TransformStream` depending on constructor argument). If such refactoring takes place, these tests will check that `Proxy` was implemented correctly and maintain backwards compatibility. --- test/core.test.js | 17 +++++++++++++++++ test/normalization.test.js | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/test/core.test.js b/test/core.test.js index 4445bcf..7cde8e5 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -293,6 +293,12 @@ suite('CSVReader', { concurrency: true }, () => { const c = await s.getReader().read().then(({ value }) => value); deepStrictEqual(c, undefined); }); + test('works with instanceof', { concurrency: true }, async () => { + const path = new URL('./data/simple.csv', import.meta.url); + const s = new CSVReader(path); + deepStrictEqual(s instanceof CSVReader, true); + await s.cancel(); + }); }); suite('CSVTransformer', { concurrency: true }, () => { @@ -547,6 +553,10 @@ suite('CSVTransformer', { concurrency: true }, () => { ])); deepStrictEqual(fn.mock.callCount(), 2); }); + test('works with instanceof', { concurrency: true }, async () => { + const s = new CSVTransformer(r => r); + deepStrictEqual(s instanceof CSVTransformer, true); + }); }); suite('CSVWriter', { concurrency: true }, () => { @@ -708,6 +718,13 @@ suite('CSVWriter', { concurrency: true }, () => { await rejects(writer.write('b')); await new CSVReader(temp).pipeTo(csvStreamEqualWritable([['a']])); }); + test('works with instanceof', { concurrency: true }, async (t) => { + const temp = await createTempFile(); + t.after(async () => await unlink(temp)); + const s = new CSVWriter(temp); + deepStrictEqual(s instanceof CSVWriter, true); + await s.abort(); + }); }); suite('CSVWriter status', { concurrency: true }, () => { diff --git a/test/normalization.test.js b/test/normalization.test.js index 620b4cf..bef63dd 100644 --- a/test/normalization.test.js +++ b/test/normalization.test.js @@ -461,6 +461,10 @@ suite('CSVNormalizer', { concurrency: true }, () => { ['a', 'b'] ])); }); + test('works with instanceof', { concurrency: true }, async () => { + const s = new CSVNormalizer([]); + deepStrictEqual(s instanceof CSVNormalizer, true); + }); }); suite('CSVDenormalizer', { concurrency: true }, () => { @@ -488,6 +492,10 @@ suite('CSVDenormalizer', { concurrency: true }, () => { ['a2', 'b2'] ])); }); + test('works with instanceof', { concurrency: true }, async () => { + const s = new CSVDenormalizer(); + deepStrictEqual(s instanceof CSVDenormalizer, true); + }); }); suite('CSVNormalizer and CSVDenormalizer end-to-end', { concurrency: true }, () => { -- 2.47.3