Skip to content

Instantly share code, notes, and snippets.

@simon-johansson
Last active March 9, 2018 10:56
Show Gist options
  • Save simon-johansson/3be2f96affd6e152d371d18fb96ed362 to your computer and use it in GitHub Desktop.
Save simon-johansson/3be2f96affd6e152d371d18fb96ed362 to your computer and use it in GitHub Desktop.
Mapper for vs code extension CodeMap used to outline Jest tests
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const reg = /((^it)|(^test)|(^describe))((\.skip)|(\.only))?\(['"](.*)['"]/
const skipPrefix = '⚪️ ';
const isolatedPrefix = '🔵 ';
const descriptionIcon = 'level3';
const testIcon = 'none';
const getMatch = (line) => line.trimLeft().match(reg);
const isTest = (line) => getMatch(line)[2] || getMatch(line)[3];
const isDescription = (line) => getMatch(line)[4];
const getText = (line) => getMatch(line)[8];
const isSkipped = (line) => getMatch(line)[6];
const isIsolated = (line) => getMatch(line)[7];
class mapper {
static read_all_lines(file) {
let text = fs.readFileSync(file, 'utf8');
return text.split(/\r?\n/g);
}
static generate(file) {
let members = [];
try {
let line_num = 0;
let image_index = 1;
mapper
.read_all_lines(file)
.forEach(line => {
line_num++;
if (getMatch(line)) {
let text = getText(line);
let icon = descriptionIcon;
if (isSkipped(line)) text = skipPrefix + text;
if (isIsolated(line)) text = isolatedPrefix + text;
if (isTest(line)) {
icon = testIcon;
text = ' ' + text;
}
members.push(`${text}|${line_num}|${icon}`);
}
});
}
catch (error) {
}
return members;
}
}
exports.mapper = mapper;
@simon-johansson
Copy link
Author

This is a dedicated mapper used with the vs code extension CodeMap to outline Jest tests in the sidebar. Should work (but I have not tested) with other test frameworks that use the same syntax (describe/it/test).

Instructions of how to add the dedicated mapper:
https://github.com/oleg-shilo/codemap.vscode/wiki/Adding-custom-mappers#mapping-with-a-dedicated-mapper

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment