Skip to content

fewlines-education/camp2-helpers

Repository files navigation

Camp2 Helpers

Installation

To install it, add this in your package.json file:

"dependencies": {
  "camp2-helpers": "fewlines-education/camp2-helpers#main"
}

Utility functions

expectMessage

import { expectMessage } from "camp2-helpers";

This function modifies the jest expect function and wrap it in a custom package in order to give an extra option to add a message when jest tests fails in TypeScript. It is based the package jest-expect-message for custom use.

You need to install jest for expect to be a global variable.

Reference

The function expectMessage(value, message, style) takes three arguments:

  • value, the value tested against a matcher. It is the same argument referenced in the Jest documentation.
  • message, a string that is printed only if the expect fails.
  • style, either:
    • a string either being "log", "warning", or "error". They represent default styles given to the messages.
      • log
      • warning
      • error
    • an object composed of three strings changing respectively the foreground color, background color and effect of the message printed.
    {
      fg?: string;
      bg?: string;
      effects?: string;
    }

Example

With pre-defined style:

test("simple test", () => {
  expectMessage(1 + 1, "This is an error message", "error").toBe(11);
});

// This test will fail and print "This is an error message" in yellow with a red background.

With your own style:

test("simple test", () => {
  expectMessage(1 + 1, "This is my own error message", {fg: "magenta", bg: "cyan", effects: "reverse"}).toBe(11);
});

// This test will fail and print "This my error message" in magenta with a cyan background and reverse !.

Documentation

Those are the different values you can use in the style object:

foreground background effect
black black reset
red red bright
green green dim
yellow yellow underscore
blue blue blink
magenta magenta reverse
cyan cyan hidden
white white

readCode

import { readCode } from "camp2-helpers";

This function uses fs.readFile to parse JS files and returns the code as a string.

Reference

The function readCode(path) takes a one argument and returns a promise with a string.

  • path, a string refering to the absolute path of a file.

E.g.

import { readCode } from "camp2-helpers";

const code = await readCode("path/of/your/file");

It can be used for testing with jest the following way.

// index.js
const hello = "Hello World!";
// index.test.js
const readcode = require("camp2-helpers");

let code;
beforeAll(() => {
  // Loads the student's code
  code = await readcode("./index.js");

  return code;
});

test("variable 'hello' contains 'Hello World!'", () => {
  return code.then((code) => {
    // Run the code then call the 'hello' variable.
    const hello = eval(code + "; hello;");

    expect(hello).toBe("Hello World!");
  });
});

astNodeParser

import { astNodeParser } from "camp2-helpers";

This function is a parser extending the TypeScript parser from recast. It creates a tree of nodes for every section and element of your code. You can read recast documentation for more information about the tree parser.

Reference

  • Cast file into string:
const code = await readCode("./path/to/file.ts");
  • Pass the string to the parser, and get the node:
// Class node example
const classNode = findNode(code, "classNode");
  • Search deeper into the node:
const classProperty = findNode(classNode, "classProperty");

You can search by name, class member name (e.g. "constructor").

To prevent breaking the tests in case of searching something that is not in the ast, the function returns "undefined".

⚠️ This is a WIP, if you probably should investigate the ast if you can't find what you are looking for.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •