@cloudpss/ubjson
Version:
Opinionated UBJSON encoder/decoder for CloudPSS.
33 lines (30 loc) • 1.06 kB
JavaScript
import { resetEnv } from '../.utils.js';
import { INPUTS, EXPECTED } from './.data.js';
import { encode, decode } from '../../dist/index.js';
describe('no encodeInto', () => {
// eslint-disable-next-line @typescript-eslint/unbound-method
const { encodeInto } = TextEncoder.prototype;
beforeAll(() => {
// @ts-expect-error 移除 encodeInto 以测试兼容性
TextEncoder.prototype.encodeInto = undefined;
resetEnv();
});
afterAll(() => {
TextEncoder.prototype.encodeInto = encodeInto;
resetEnv();
});
it.each(Object.keys(INPUTS))('%s', (name) => {
const input = INPUTS[name];
const expected = EXPECTED[name] ?? input;
if (expected instanceof Error) {
expect(() => {
const encoded = encode(input);
decode(encoded);
}).toThrow(expected);
} else {
const encoded = encode(input);
const decoded = decode(encoded);
expect(decoded).toEqual(expected);
}
});
});