Skip to content

Commit

Permalink
test: move some unit tests to use node:test (withastro#10070)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Feb 9, 2024
1 parent 0b5685a commit 8f37cf7
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 153 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { resolveConfig } from '../../../dist/core/config/index.js';
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';

describe('resolveConfig', () => {
it('resolves relative inline root correctly', async () => {
Expand All @@ -13,6 +14,6 @@ describe('resolveConfig', () => {
'dev'
);
const expectedRoot = path.join(process.cwd(), 'relative/path/');
expect(fileURLToPath(astroConfig.root)).to.equal(expectedRoot);
assert.equal(fileURLToPath(astroConfig.root), expectedRoot);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import { fileURLToPath } from 'node:url';
import { flagsToAstroInlineConfig } from '../../../dist/cli/flags.js';
import { resolveConfig } from '../../../dist/core/config/index.js';
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';

const cwd = fileURLToPath(new URL('../../fixtures/config-host/', import.meta.url));

Expand All @@ -24,7 +25,7 @@ describe('config.server', () => {
host: true,
});

expect(astroConfig.server.host).to.equal(true);
assert.equal(astroConfig.server.host, true);
});
});

Expand All @@ -37,7 +38,7 @@ describe('config.server', () => {
root: fileURLToPath(projectRootURL),
config: configFileURL,
});
expect(astroConfig.server.port).to.equal(8080);
assert.equal(astroConfig.server.port, 8080);
});
});

Expand All @@ -49,7 +50,7 @@ describe('config.server', () => {
root: fileURLToPath(projectRootURL),
config: configFileURL,
});
expect(astroConfig.server.port).to.equal(8080);
assert.equal(astroConfig.server.port, 8080);
});
});

Expand All @@ -62,9 +63,9 @@ describe('config.server', () => {
root: fileURLToPath(projectRootURL),
config: configFileURL,
});
expect(false).to.equal(true, 'this should not have resolved');
assert.equal(false, true, 'this should not have resolved');
} catch (err) {
expect(err.message).to.match(/Unable to resolve/);
assert.equal(err.message.includes('Unable to resolve'), true);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import { loadTSConfig, updateTSConfigForFramework } from '../../../dist/core/config/index.js';
Expand All @@ -10,31 +11,31 @@ describe('TSConfig handling', () => {
it('can load tsconfig.json', async () => {
const config = await loadTSConfig(cwd);

expect(config).to.not.be.undefined;
assert.equal(config !== undefined, true);
});

it('can resolve tsconfig.json up directories', async () => {
const config = await loadTSConfig(cwd);

expect(config).to.not.be.undefined;
expect(config.tsconfigFile).to.equal(path.join(cwd, 'tsconfig.json'));
expect(config.tsconfig.files).to.deep.equal(['im-a-test']);
assert.equal(config !== undefined, true);
assert.equal(config.tsconfigFile, path.join(cwd, 'tsconfig.json'));
assert.deepEqual(config.tsconfig.files, ['im-a-test']);
});

it('can fallback to jsconfig.json if tsconfig.json does not exists', async () => {
const config = await loadTSConfig(path.join(cwd, 'jsconfig'));

expect(config).to.not.be.undefined;
expect(config.tsconfigFile).to.equal(path.join(cwd, 'jsconfig', 'jsconfig.json'));
expect(config.tsconfig.files).to.deep.equal(['im-a-test-js']);
assert.equal(config !== undefined, true);
assert.equal(config.tsconfigFile, path.join(cwd, 'jsconfig', 'jsconfig.json'));
assert.deepEqual(config.tsconfig.files, ['im-a-test-js']);
});

it('properly return errors when not resolving', async () => {
const invalidConfig = await loadTSConfig(path.join(cwd, 'invalid'));
const missingConfig = await loadTSConfig(path.join(cwd, 'missing'));

expect(invalidConfig).to.equal('invalid-config');
expect(missingConfig).to.equal('missing-config');
assert.equal(invalidConfig, 'invalid-config');
assert.equal(missingConfig, 'missing-config');
});
});

Expand All @@ -43,15 +44,15 @@ describe('TSConfig handling', () => {
const config = await loadTSConfig(cwd);
const updatedConfig = updateTSConfigForFramework(config.tsconfig, 'react');

expect(config.tsconfig).to.not.equal('react-jsx');
expect(updatedConfig.compilerOptions.jsx).to.equal('react-jsx');
assert.notEqual(config.tsconfig, 'react-jsx');
assert.equal(updatedConfig.compilerOptions.jsx, 'react-jsx');
});

it('produce no changes on invalid frameworks', async () => {
const config = await loadTSConfig(cwd);
const updatedConfig = updateTSConfigForFramework(config.tsconfig, 'doesnt-exist');

expect(config.tsconfig).to.deep.equal(updatedConfig);
assert.deepEqual(config.tsconfig, updatedConfig);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { expect } from 'chai';
import { z } from 'zod';
import stripAnsi from 'strip-ansi';
import { formatConfigErrorMessage } from '../../../dist/core/messages.js';
import { validateConfig } from '../../../dist/core/config/config.js';
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';

describe('Config Validation', () => {
it('empty user config is valid', async () => {
expect(() => validateConfig({}, process.cwd()).catch((err) => err)).not.to.throw();
assert.doesNotThrow(() => validateConfig({}, process.cwd()).catch((err) => err));
});

it('Zod errors are returned when invalid config is used', async () => {
const configError = await validateConfig({ site: 42 }, process.cwd()).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
assert.equal(configError instanceof z.ZodError, true);
});

it('A validation error can be formatted correctly', async () => {
const configError = await validateConfig({ site: 42 }, process.cwd()).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
assert.equal(configError instanceof z.ZodError, true);
const formattedError = stripAnsi(formatConfigErrorMessage(configError));
expect(formattedError).to.equal(
assert.equal(
formattedError,
`[config] Astro found issue(s) with your configuration:
! site Expected string, received number.`
);
Expand All @@ -30,9 +32,10 @@ describe('Config Validation', () => {
build: { format: 'invalid' },
};
const configError = await validateConfig(veryBadConfig, process.cwd()).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
assert.equal(configError instanceof z.ZodError, true);
const formattedError = stripAnsi(formatConfigErrorMessage(configError));
expect(formattedError).to.equal(
assert.equal(
formattedError,
`[config] Astro found issue(s) with your configuration:
! integrations.0 Expected object, received number.
! build.format Invalid input.`
Expand All @@ -44,18 +47,18 @@ describe('Config Validation', () => {
{ integrations: [0, false, null, undefined] },
process.cwd()
);
expect(result.integrations).to.deep.equal([]);
assert.deepEqual(result.integrations, []);
});
it('normalizes "integration" values', async () => {
const result = await validateConfig({ integrations: [{ name: '@astrojs/a' }] }, process.cwd());
expect(result.integrations).to.deep.equal([{ name: '@astrojs/a', hooks: {} }]);
assert.deepEqual(result.integrations, [{ name: '@astrojs/a', hooks: {} }]);
});
it('flattens array "integration" values', async () => {
const result = await validateConfig(
{ integrations: [{ name: '@astrojs/a' }, [{ name: '@astrojs/b' }, { name: '@astrojs/c' }]] },
process.cwd()
);
expect(result.integrations).to.deep.equal([
assert.deepEqual(result.integrations, [
{ name: '@astrojs/a', hooks: {} },
{ name: '@astrojs/b', hooks: {} },
{ name: '@astrojs/c', hooks: {} },
Expand All @@ -66,14 +69,15 @@ describe('Config Validation', () => {
{ integrations: [null, undefined, false, '', ``] },
process.cwd()
).catch((err) => err);
expect(configError).to.be.not.instanceOf(Error);
assert.equal(configError instanceof Error, false);
});
it('Error when outDir is placed within publicDir', async () => {
const configError = await validateConfig({ outDir: './public/dist' }, process.cwd()).catch(
(err) => err
);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
'The value of `outDir` must not point to a path within the folder set as `publicDir`, this will cause an infinite loop'
);
});
Expand All @@ -89,8 +93,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
'The default locale `en` is not present in the `i18n.locales` array.'
);
});
Expand All @@ -111,8 +116,8 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal('Array must contain at least 1 element(s)');
assert.equal(configError instanceof z.ZodError, true);
assert.equal(configError.errors[0].message, 'Array must contain at least 1 element(s)');
});

it('errors if the default locale is not in path', async () => {
Expand All @@ -131,8 +136,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
'The default locale `uk` is not present in the `i18n.locales` array.'
);
});
Expand All @@ -150,8 +156,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
"The locale `it` value in the `i18n.fallback` record doesn't exist in the `i18n.locales` array."
);
});
Expand All @@ -169,8 +176,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
"The locale `it` key in the `i18n.fallback` record doesn't exist in the `i18n.locales` array."
);
});
Expand All @@ -188,8 +196,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
"You can't use the default locale as a key. The default locale can only be used as value."
);
});
Expand All @@ -208,8 +217,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
'The option `i18n.redirectToDefaultLocale` is only useful when the `i18n.prefixDefaultLocale` is set to `true`. Remove the option `i18n.redirectToDefaultLocale`, or change its value to `true`.'
);
});
Expand All @@ -228,8 +238,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
"The locale `lorem` key in the `i18n.domains` record doesn't exist in the `i18n.locales` array."
);
});
Expand All @@ -248,8 +259,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
"The domain value must be a valid URL, and it has to start with 'https' or 'http'."
);
});
Expand All @@ -268,8 +280,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
"The domain value must be a valid URL, and it has to start with 'https' or 'http'."
);
});
Expand All @@ -288,8 +301,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
"The URL `https://www.example.com/blog/page/` must contain only the origin. A subsequent pathname isn't allowed here. Remove `/blog/page/`."
);
});
Expand All @@ -311,8 +325,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
"The option `site` isn't set. When using the 'domains' strategy for `i18n`, `site` is required to create absolute URLs for locales that aren't mapped to a domain."
);
});
Expand All @@ -335,8 +350,9 @@ describe('Config Validation', () => {
},
process.cwd()
).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.errors[0].message).to.equal(
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
'Domain support is only available when `output` is `"server"`.'
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fileURLToPath } from 'node:url';
import { expect } from 'chai';
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';
import { createFs, runInContainer } from '../test-utils.js';

const root = new URL('../../fixtures/tailwindcss-ts/', import.meta.url);
Expand All @@ -19,7 +20,8 @@ describe('Astro config formats', () => {
);

await runInContainer({ fs, inlineConfig: { root: fileURLToPath(root) } }, () => {
expect(true).to.equal(
assert.equal(
true,
true,
'We were able to get into the container which means the config loaded.'
);
Expand Down
Loading

0 comments on commit 8f37cf7

Please sign in to comment.