react / wstein / node_modules / jest-cli / node_modules / node-haste / lib / loader / JSTestLoader.js
80668 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/15var inherits = require('util').inherits;16var path = require('path');1718var docblock = require('../parse/docblock');19var extract = require('../parse/extract');20var ResourceLoader = require('./ResourceLoader');21var JSTest = require('../resource/JSTest');2223function escapeStrForRegex(str) {24return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');25}2627/**28* @class Loads and parses __tests__ / *.js files29*30* @extends {ResourceLoader}31*/32function JSTestLoader(options) {33ResourceLoader.call(this, options);3435this.pathRe = new RegExp(36'(?:/|^)__tests__/' +37(this.options.matchSubDirs ? '(.+)' : '([^/]+)') +38'(' + this.getExtensions().map(escapeStrForRegex).join('|') + ')$'39);40}41inherits(JSTestLoader, ResourceLoader);42JSTestLoader.prototype.path = __filename;4344var requireRe = /(?:\brequire|\.map)\s*\(\s*[\'"]([^"\']+)["\']\s*\)/g;4546JSTestLoader.prototype.getResourceTypes = function() {47return [JSTest];48};4950JSTestLoader.prototype.getExtensions = function() {51return this.options.extensions || ['.js'];52};535455/**56* Initialize a resource with the source code and configuration57* Loader can parse, gzip, minify the source code to build the resulting58* Resource value object59*60* @protected61* @param {String} filePath resource being built62* @param {ProjectConfiguration} configuration configuration for the path63* @param {String} sourceCode64* @param {Function} callback65*/66JSTestLoader.prototype.loadFromSource =67function(filePath, configuration, sourceCode, messages, callback) {6869var test = new JSTest(filePath);70var match = filePath.match(this.pathRe);71test.id = match ? match[1] : path.basename(filePath, '.js');7273docblock.parse(docblock.extract(sourceCode)).forEach(function(pair) {74var name = pair[0];75var value = pair[1];7677switch (name) {78case 'emails':79test.contacts = value.split(/\s/);80break;81default:82// do nothing83}84});8586test.requiredModules = extract.strings(sourceCode, requireRe, 1);87callback(messages, test);88};8990/**91* Only match __tests__ / *.js files92* @static93* @param {String} filePath94* @return {Boolean}95*/96JSTestLoader.prototype.matchPath = function(filePath) {97return this.pathRe.test(filePath);98};99100101module.exports = JSTestLoader;102103104