Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiazya committed Sep 14, 2024
1 parent 095a7a8 commit 1cdbd6b
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 37 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/.dynamic-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
- run: pnpm test run
- run: pnpm test run --project node


test_deno:
Expand All @@ -69,7 +69,7 @@ jobs:
deno-version: v1.x
- run: |
deno eval '
import { parse } from "./dist/es/web-csv-toolbox.js";
import { parse } from "./packages/web-csv-toolbox/dist/web-csv-toolbox.js";
const csv = `name,age
Alice,42
Expand All @@ -94,7 +94,7 @@ jobs:
with:
skip-rust-setup: 'true'
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
- run: npm test run -- --browser.name=${{ matrix.browsers }} --browser.headless
- run: pnpm test run --project browser --browser.name=${{ matrix.browsers }} --browser.headless

test_macos_browser:
runs-on: macos-latest
Expand All @@ -118,8 +118,7 @@ jobs:
# - if: matrix.browsers == 'safari'
# run: sudo safaridriver --enable
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
- run: pnpm test run -- --browser.name=${{ matrix.browsers }} --browser.headless

- run: pnpm test run --project browser --browser.name=${{ matrix.browsers }} --browser.headless

test_windows_browser:
runs-on: windows-latest
Expand All @@ -137,4 +136,4 @@ jobs:
with:
skip-rust-setup: 'true'
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
- run: pnpm test run -- --browser.name=${{ matrix.browsers }} --browser.headless
- run: pnpm test run --project browser --browser.name=${{ matrix.browsers }} --browser.headless
24 changes: 12 additions & 12 deletions packages/parser/src/lib/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("parse function", () => {
fc.gen().map((g) => {
const header = g(FC.header, {
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const EOL = g(FC.eol);
Expand All @@ -96,14 +96,14 @@ describe("parse function", () => {
maxLength: header.length,
},
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
}),
// Last row is not empty for testing.
g(FC.row, {
fieldConstraints: {
minLength: 1,
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down Expand Up @@ -139,7 +139,7 @@ describe("parse function", () => {
fc.gen().map((g) => {
const header = g(FC.header, {
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const EOL = g(FC.eol);
Expand All @@ -154,14 +154,14 @@ describe("parse function", () => {
maxLength: header.length,
},
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
}),
// Last row is not empty for testing.
g(FC.row, {
fieldConstraints: {
minLength: 1,
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down Expand Up @@ -252,7 +252,7 @@ describe("parse function", () => {
fc.gen().map((g) => {
const header = g(FC.header, {
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const EOL = g(FC.eol);
Expand All @@ -267,14 +267,14 @@ describe("parse function", () => {
maxLength: header.length,
},
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
}),
// Last row is not empty for testing.
g(FC.row, {
fieldConstraints: {
minLength: 1,
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down Expand Up @@ -313,7 +313,7 @@ describe("parse function", () => {
fc.gen().map((g) => {
const header = g(FC.header, {
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const EOL = g(FC.eol);
Expand All @@ -328,14 +328,14 @@ describe("parse function", () => {
maxLength: header.length,
},
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
}),
// Last row is not empty for testing.
g(FC.row, {
fieldConstraints: {
minLength: 1,
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/src/lib/parseBinary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("parseBinary function", () => {
const header = g(FC.header, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const BOM = g(fc.boolean);
Expand All @@ -28,7 +28,7 @@ describe("parseBinary function", () => {
const csvData = g(FC.csvData, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/src/lib/parseResponse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("parseRequest function", () => {
const header = g(FC.header, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const BOM = g(fc.boolean);
Expand All @@ -50,7 +50,7 @@ describe("parseRequest function", () => {
const csvData = g(FC.csvData, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/src/lib/parseResponseToStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ describe("parseResponseToStream", () => {
const header = g(FC.header, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const EOL = g(FC.eol);
const csvData = g(FC.csvData, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down
8 changes: 4 additions & 4 deletions packages/parser/src/lib/parseUint8ArrayStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ describe("parseUint8ArrayStream function", () => {
const header = g(FC.header, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const EOL = g(FC.eol);
const csvData = g(FC.csvData, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down Expand Up @@ -89,14 +89,14 @@ describe("parseUint8ArrayStream function", () => {
const header = g(FC.header, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const EOL = g(FC.eol);
const csvData = g(FC.csvData, {
// TextEncoderStream can't handle utf-16 string.
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/lib/utils/getOptionsFromResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function getOptionsFromResponse<Header extends ReadonlyArray<string>>(
options: ParseBinaryOptions<Header> = {},
): ParseBinaryOptions<Header> {
const { headers } = response;
const contentType = headers.get("content-type") ?? "text/csv";
const contentType = headers.get("content-type")!;
const mime = parseMime(contentType);
if (mime.type !== "text/csv") {
throw new RangeError(`Invalid mime type: "${contentType}"`);
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/lib/parseMime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface ParseMimeResult {
};
}

export function parseMime(contentType: string) {
export function parseMime(contentType: string): ParseMimeResult {
const [type, ...parameters] = contentType.split(";");
const result: ParseMimeResult = {
type: type.trim(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import fc from "fast-check";
import { beforeAll, describe, expect, it } from "vitest";

import { escapeField } from "@web-csv-toolbox/common";
import { loadWASM } from "@web-csv-toolbox/wasm";

import { FC } from "#/tests/utils/helper";

import { parseStringToArraySync } from "./wasm";
import { loadWASM, parseStringToArraySync } from "./wasm.browser";

describe("parseStringToArraySync", async () => {
beforeAll(async () => {
Expand All @@ -19,13 +18,13 @@ describe("parseStringToArraySync", async () => {
fc.gen().map((g) => {
const header = g(FC.header, {
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
});
const EOL = g(FC.eol);
const csvData = g(FC.csvData, {
fieldConstraints: {
kindExcludes: ["string16bits"],
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
Expand Down
83 changes: 83 additions & 0 deletions packages/wasm/src/parseStringToArraySync.node.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import fc from "fast-check";
import { beforeAll, describe, expect, it } from "vitest";

import { escapeField } from "@web-csv-toolbox/common";

import { FC } from "#/tests/utils/helper";

import { loadWASM, parseStringToArraySync } from "./wasm";

describe("parseStringToArraySync", async () => {
beforeAll(async () => {
await loadWASM();
});

it("should parse CSV string to record of arrays", async () => {
await fc.assert(
fc.asyncProperty(
fc.gen().map((g) => {
const header = g(FC.header, {
fieldConstraints: {
unit: "grapheme",
},
});
const EOL = g(FC.eol);
const csvData = g(FC.csvData, {
fieldConstraints: {
unit: "grapheme",
},
columnsConstraints: {
minLength: header.length,
maxLength: header.length,
},
});
const EOF = g(fc.boolean);
const csv = [
header.map((v) => escapeField(v, { quote: true })).join(","),
...csvData.map((row) =>
row.map((v) => escapeField(v, { quote: true })).join(","),
),
...(EOF ? [""] : []),
].join(EOL);
const data =
csvData.length >= 1
? csvData.map((row) =>
Object.fromEntries(row.map((v, i) => [header[i], v])),
)
: [];
return { data, csv, header };
}),
async ({ data, csv }) => {
let i = 0;
const result = parseStringToArraySync(csv);
expect(result.length).toEqual(data.length);
for (const record of result) {
expect(data[i++]).toEqual(record);
}
},
),
);
});

it("should throw error when delimiter is not a single character", async () => {
const csv = "a,b,c\n1,2,3";

expect(() =>
parseStringToArraySync(csv, { delimiter: "ab" }),
).toThrowErrorMatchingInlineSnapshot(
// biome-ignore lint/style/noUnusedTemplateLiteral: This is a snapshot
`[RangeError: delimiter must be a single character]`,
);
});

it("should throw error when quotation is not double quote", async () => {
const csv = "a,b,c\n1,2,3";

expect(() =>
parseStringToArraySync(csv, { quotation: "'" }),
).toThrowErrorMatchingInlineSnapshot(
// biome-ignore lint/style/noUnusedTemplateLiteral: This is a snapshot
`[RangeError: Invalid quotation, must be double quote on WASM.]`,
);
});
});
4 changes: 1 addition & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ export default defineConfig({
plugins: [wasm(), tsconfigPaths() as any],
test: {
setupFiles: [".config/vitest.setup.ts"],
browser: {
name: "chrome",
},
coverage: {
enabled: true,
provider: "istanbul", // use istanbul for browser coverage
include: ["packages/**/*.ts"],
},
Expand Down
Loading

0 comments on commit 1cdbd6b

Please sign in to comment.