Skip to content

Commit

Permalink
added esm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed May 23, 2024
1 parent 99fc40f commit ebc1553
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"scripts": {
"clean": "shx rm -f esm/index.mjs lib/nuid.d.ts lib/nuid.js lib/nuid.js.map",
"build": "npm run clean && deno bundle src/nuid.ts esm/index.mjs && tsc",
"test": "npm run build && node --test",
"test": "npm run build && node --test test/basics.js && deno test",
"prepack": "npm run build"
},
"engines": {
Expand Down
69 changes: 69 additions & 0 deletions test/basics_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2016-2024 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { next, nuid, reset } from "../esm/index.mjs";
import {
assert,
assertEquals,
assertExists,
assertNotEquals,
} from "jsr:@std/assert";

Deno.test("global nuid should not be null", () => {
assertExists(nuid);
nuid.next();
assertExists(nuid.buf);
assert(nuid.buf.length > 0);
assertExists(nuid.seq);
assert(nuid.seq > 0);
assertExists(nuid.inc);
});

Deno.test("duplicate nuids", () => {
const m: Record<string, boolean> = {};
// make this really big when testing, for normal runs small
for (let i = 0; i < 10000; i++) {
const k = nuid.next();
assertEquals(m[k], undefined);
m[k] = true;
}
});

Deno.test("roll seq", () => {
const a = nuid.buf.slice(12);
nuid.next();
const b = nuid.buf.slice(12);
assertNotEquals(a, b);
});

Deno.test("roll pre", () => {
nuid.seq = 3656158440062976 + 1;
const a = nuid.buf.slice(0, 12);
nuid.next();
const b = nuid.buf.slice(0, 12);
assertNotEquals(a, b);
});

Deno.test("reset should reset", () => {
const a = nuid.buf.slice(0, 12);
reset();
const b = nuid.buf.slice(0, 12);
assertNotEquals(a, b);
});

Deno.test("reset and next are fn", () => {
assertEquals(typeof reset, "function");
assertEquals(typeof next, "function");
});

0 comments on commit ebc1553

Please sign in to comment.