Skip to content

Commit

Permalink
refactor(test): replaces chai with node:assert and chai-json-schema w…
Browse files Browse the repository at this point in the history
…ith ajv (#823)

## Description

- replaces the chai assertion library use with their native node:assert counterparts where this was doable in a reasonable timeframe
- replaces the chai-json-schema chai plugin with a straight use of ajv
- removes chai-json-schema from the manifest as it ain't used no more

The more laborious parts we'll do in a separate PR, likely with the help
of a codemod of sorts.

## Motivation and Context

- less dependencies to manage
- node:assert ships with node anyway and is good enough™️ 

## How Has This Been Tested?

- [x] green ci


## Types of changes

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Documentation only change
- [x] Refactor (non-breaking change which fixes an issue without
changing functionality)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist

- [x] 📖

  - My change doesn't require a documentation update, or ...
  - it _does_ and I have updated it

- [x] ⚖️
- The contribution will be subject to [The MIT
license](https://github.com/sverweij/dependency-cruiser/blob/main/LICENSE),
and I'm OK with that.
  - The contribution is my own original work.
- I am ok with the stuff in
[**CONTRIBUTING.md**](https://github.com/sverweij/dependency-cruiser/blob/main/.github/CONTRIBUTING.md).
  • Loading branch information
sverweij authored Jul 28, 2023
1 parent f6767ea commit 64398da
Show file tree
Hide file tree
Showing 82 changed files with 1,555 additions and 1,438 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@
"@vue/compiler-sfc": "3.3.4",
"c8": "8.0.0",
"chai": "4.3.7",
"chai-json-schema": "1.5.1",
"coffeescript": "2.7.0",
"eslint": "8.45.0",
"eslint-config-moving-meadow": "4.0.2",
Expand Down
22 changes: 10 additions & 12 deletions test/api.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint disable because import/no-unresolved,node/no-missing-import don't
// know about (local) module imports yet
/* eslint-disable import/no-unresolved,node/no-missing-import */
import { expect } from "chai";
import { strictEqual } from "node:assert";
import satisfies from "semver/functions/satisfies.js";
import dependencyCruiser from "dependency-cruiser";
import extractBabelConfig from "dependency-cruiser/config-utl/extract-babel-config";
Expand All @@ -15,29 +15,27 @@ import extractWebpackResolveConfig from "dependency-cruiser/config-utl/extract-w
if (satisfies(process.versions.node, "^12.19 || >=14.7")) {
describe("[E] api from esm (on node ^12.19 || >= 14.7)", () => {
it("exposes dependency-cruiser main with some functions", () => {
expect(typeof dependencyCruiser).to.equal("object");
expect(typeof dependencyCruiser.cruise).to.equal("function");
expect(typeof dependencyCruiser.format).to.equal("function");
expect(Array.isArray(dependencyCruiser.allExtensions)).to.equal(true);
expect(typeof dependencyCruiser.getAvailableTranspilers).to.equal(
"function",
);
strictEqual(typeof dependencyCruiser, "object");
strictEqual(typeof dependencyCruiser.cruise, "function");
strictEqual(typeof dependencyCruiser.format, "function");
strictEqual(Array.isArray(dependencyCruiser.allExtensions), true);
strictEqual(typeof dependencyCruiser.getAvailableTranspilers, "function");
});

it("exposes an extract-babel-config function", () => {
expect(typeof extractBabelConfig).to.equal("function");
strictEqual(typeof extractBabelConfig, "function");
});

it("exposes an extract-depcruise-config function", () => {
expect(typeof extractDepcruiseConfig).to.equal("function");
strictEqual(typeof extractDepcruiseConfig, "function");
});

it("exposes an extract-ts-config function", () => {
expect(typeof extractTsConfig).to.equal("function");
strictEqual(typeof extractTsConfig, "function");
});

it("exposes an extract-webpack-resolve-config function", () => {
expect(typeof extractWebpackResolveConfig).to.equal("function");
strictEqual(typeof extractWebpackResolveConfig, "function");
});
});
}
160 changes: 78 additions & 82 deletions test/cache/cache.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { deepStrictEqual, strictEqual } from "node:assert";
import { rmSync } from "node:fs";
import { join } from "node:path";
import { expect } from "chai";
import { isDeepStrictEqual } from "node:util";
import { describe } from "mocha";
import Cache from "../../src/cache/cache.mjs";

Expand All @@ -9,34 +10,36 @@ const OUTPUTS_FOLDER = "test/cache/__outputs__/";
describe("[I] cache/cache - readCache", () => {
it("returns an empty cache when trying to read from a non-existing one", async () => {
const lCache = new Cache();
expect(await lCache.read("this/folder/does/not-exist")).to.deep.equal({
deepStrictEqual(await lCache.read("this/folder/does/not-exist"), {
modules: [],
summary: {},
});
});

it("returns an empty cache when trying to read from a file that is invalid JSON", async () => {
const lCache = new Cache();
expect(
deepStrictEqual(
await lCache.read("test/cache/__mocks__/cache/invalid-json"),
).to.deep.equal({
modules: [],
summary: {},
});
{
modules: [],
summary: {},
},
);
});

it("returns the contents of the cache when trying to read from an existing, valid json", async () => {
const lCache = new Cache();
expect(
deepStrictEqual(
await lCache.read("test/cache/__mocks__/cache/valid-minimal-cache"),
).to.deep.equal({
modules: [],
summary: {},
revisionData: {
SHA1: "26fc7183127945393f77c8559f28bf623babe17f",
changes: [],
{
modules: [],
summary: {},
revisionData: {
SHA1: "26fc7183127945393f77c8559f28bf623babe17f",
changes: [],
},
},
});
);
});
});

Expand All @@ -54,7 +57,7 @@ describe("[I] cache/cache - writeCache", () => {
const lCache = new Cache();

await lCache.write(lCacheFolder, lDummyCacheContents);
expect(await lCache.read(lCacheFolder)).to.deep.equal(lDummyCacheContents);
deepStrictEqual(await lCache.read(lCacheFolder), lDummyCacheContents);
});

it("writes the passed cruise options to the cache folder (folder already exists -> overwrite)", async () => {
Expand All @@ -69,9 +72,7 @@ describe("[I] cache/cache - writeCache", () => {

await lCache.write(lCacheFolder, lDummyCacheContents);
await lCache.write(lCacheFolder, lSecondDummyCacheContents);
expect(await lCache.read(lCacheFolder)).to.deep.equal(
lSecondDummyCacheContents,
);
deepStrictEqual(await lCache.read(lCacheFolder), lSecondDummyCacheContents);
});

it("writes the passed cruise options to the cache folder (which is created when it doesn't exist yet) - content based cached strategy", async () => {
Expand All @@ -86,7 +87,7 @@ describe("[I] cache/cache - writeCache", () => {
const lRevisionData = { SHA1: "dummy-sha", changes: [] };

await lCache.write(lCacheFolder, lDummyCacheContents, lRevisionData);
expect(await lCache.read(lCacheFolder)).to.deep.equal(lDummyCacheContents);
isDeepStrictEqual(await lCache.read(lCacheFolder), lDummyCacheContents);
});
});

Expand Down Expand Up @@ -115,60 +116,57 @@ describe("[I] cache/cache - canServeFromCache", () => {
const lEmptyCruiseResult = { modules: [], summary: [] };
const lCache = new Cache();

expect(
await lCache.canServeFromCache(
{ cache: { folder: lCacheFolder, strategy: "metadata" } },
lEmptyCruiseResult,
{
SHA1: "dummy-sha",
changes: [],
},
),
).to.equal(false);
const lResult = await lCache.canServeFromCache(
{ cache: { folder: lCacheFolder, strategy: "metadata" } },
lEmptyCruiseResult,
{
SHA1: "dummy-sha",
changes: [],
},
);
strictEqual(lResult, false);
});

it("returns false when the base SHA differs", async () => {
const lCacheFolder = join(OUTPUTS_FOLDER, "serve-from-cache-sha-differs");
const lCache = new Cache();
const lFound = await lCache.canServeFromCache(
{
args: "src test tools",
cache: { folder: lCacheFolder, strategy: "metadata" },
},
lMinimalCruiseResult,
{
SHA1: "another-sha",
changes: [],
},
);

expect(
await lCache.canServeFromCache(
{
args: "src test tools",
cache: { folder: lCacheFolder, strategy: "metadata" },
},
lMinimalCruiseResult,
{
SHA1: "another-sha",
changes: [],
},
),
).to.equal(false);
strictEqual(lFound, false);
});

it("returns false when a file was added", async () => {
const lCacheFolder = join(OUTPUTS_FOLDER, "serve-from-cache-file-added");
const lCache = new Cache();
const lFound = await lCache.canServeFromCache(
{
args: "src test tools",
cache: { folder: lCacheFolder, strategy: "metadata" },
},
lMinimalCruiseResult,
{
SHA1: "dummy-sha",
changes: [
{
changeType: "added",
name: "some-new-file.aap",
checksum: "dummy-checksum",
},
],
},
);

expect(
await lCache.canServeFromCache(
{
args: "src test tools",
cache: { folder: lCacheFolder, strategy: "metadata" },
},
lMinimalCruiseResult,
{
SHA1: "dummy-sha",
changes: [
{
changeType: "added",
name: "some-new-file.aap",
checksum: "dummy-checksum",
},
],
},
),
).to.equal(false);
strictEqual(lFound, false);
});

it("returns false when cache written & revision data equal & options incompatible", async () => {
Expand All @@ -177,31 +175,29 @@ describe("[I] cache/cache - canServeFromCache", () => {
"serve-from-cache-options-incompatible",
);
const lCache = new Cache();
const lFound = await lCache.canServeFromCache(
{
args: "src test tools configs",
cache: { folder: lCacheFolder, strategy: "metadata" },
},
lMinimalCruiseResult,
{ SHA1: "dummy-sha", changes: [] },
);

expect(
await lCache.canServeFromCache(
{
args: "src test tools configs",
cache: { folder: lCacheFolder, strategy: "metadata" },
},
lMinimalCruiseResult,
{ SHA1: "dummy-sha", changes: [] },
),
).to.equal(false);
strictEqual(lFound, false);
});

it("returns true when cache written & revision data equal & options compatible", async () => {
const lCache = new Cache();
const lFound = await lCache.canServeFromCache(
{
args: "src test tools",
cache: { folder: lOriginalCacheFolder, strategy: "metadata" },
},
lMinimalCruiseResult,
{ SHA1: "dummy-sha", changes: [] },
);

expect(
await lCache.canServeFromCache(
{
args: "src test tools",
cache: { folder: lOriginalCacheFolder, strategy: "metadata" },
},
lMinimalCruiseResult,
{ SHA1: "dummy-sha", changes: [] },
),
).to.equal(true);
strictEqual(lFound, true);
});
});
Loading

0 comments on commit 64398da

Please sign in to comment.