@gentrace/core
Version:
Core Gentrace Node.JS library
142 lines (141 loc) • 6.43 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { resetGlobalGentraceApi } from "../providers/init";
import { getParamNames } from "../providers/utils";
describe("Parser tests for param extraction", () => {
const OLD_ENV = process.env;
afterAll(() => {
process.env = OLD_ENV;
});
beforeEach(() => {
jest.resetModules();
process.env = {};
resetGlobalGentraceApi();
});
describe("constructor", () => {
it("should get all named identifiers", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames((a, b, c) => {
return a;
});
const expected = ["a", "b", "c"];
expect(params).toEqual(expected);
});
it("should create placeholder name for destructured value", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(({ another }, b, c) => {
return another;
});
const expected = ["param0", "b", "c"];
expect(params).toEqual(expected);
});
it("should create placeholder name for destructured value with assigned default value", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(({ another } = { another: "Testing" }, b, c) => {
return another;
});
const expected = ["param0", "b", "c"];
expect(params).toEqual(expected);
});
it("should still get identifier if there's a default value assigned", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames((a = { another: "Testing" }, b, c) => {
return a;
});
const expected = ["a", "b", "c"];
expect(params).toEqual(expected);
});
it("should still get identifier if it's async", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames((a = { another: "Testing" }, b, c) => __awaiter(void 0, void 0, void 0, function* () {
return a;
}));
const expected = ["a", "b", "c"];
expect(params).toEqual(expected);
});
it("should still get identifier if it's single param with no parens", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(
// prettier-ignore
a => {
return a;
});
const expected = ["a"];
expect(params).toEqual(expected);
});
it("should still get identifier if it's async and single param with no parens", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(
// prettier-ignore
(a) => __awaiter(void 0, void 0, void 0, function* () {
return a;
}));
const expected = ["a"];
expect(params).toEqual(expected);
});
it("should still get identifier if it's non-lambda, async", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(function (a) {
return __awaiter(this, void 0, void 0, function* () {
return a;
});
});
const expected = ["a"];
expect(params).toEqual(expected);
});
it("should still get identifier if it's non-lambda, async", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(function (a, b, c) {
return __awaiter(this, void 0, void 0, function* () {
return a;
});
});
const expected = ["a", "b", "c"];
expect(params).toEqual(expected);
});
it("should still get identifiers if it's non-lambda, 3 params, 1 destructured, async", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(function ({ a }, b, c) {
return __awaiter(this, void 0, void 0, function* () {
return a;
});
});
const expected = ["param0", "b", "c"];
expect(params).toEqual(expected);
});
it("should still get identifiers if it's non-lambda, 3 params, 1 destructured, sync", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(function ({ a }, b, c) {
return a;
});
const expected = ["param0", "b", "c"];
expect(params).toEqual(expected);
});
it("should still get identifiers if it's non-lambda, 0 params, sync", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(function () { });
const expected = [];
expect(params).toEqual(expected);
});
it("should still get identifiers if it's lambda, 0 params, sync", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
const params = getParamNames(() => { });
const expected = [];
expect(params).toEqual(expected);
});
it("should get empty array if a function isn't passed in", () => {
process.env.GENTRACE_API_KEY = "some-test-api-key";
// @ts-ignore
const params = getParamNames("sdlkjf");
const expected = [];
expect(params).toEqual(expected);
});
});
});