我一直在撞墙,为了我的生命,我无法弄清问题。我正在使用Jest(ts-jest)和默认的JSDOM进行测试。
在测试中,我分别声明每个测试的文档内容。例如:
LoadModule fcgid_module modules/mod_fcgid.so
#
<IfModule fcgid_module>
FcgidInitialEnv PATH "/www/php/encoders_new;/www/php/mplayer;/www/imagemagick;/www/php/bash;F:/www/perl/perl/bin;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem"
FcgidInitialEnv SystemRoot "C:/Windows"
FcgidInitialEnv SystemDrive "C:"
FcgidInitialEnv TEMP "/www/php/tmp"
FcgidInitialEnv TMP "/www/php/tmp"
FcgidInitialEnv windir "C:/WINDOWS"
FcgidInitialEnv MIBDIRS "/www/php/x64/nts/php5.3.29VC9/extras/mibs"
FcgidInitialEnv MYSQL_HOME "/www/MySQL5.1.68/bin"
FcgidInitialEnv OPENSSL_CONF "/www/apache2.4.34x64-VC14/conf/openssl.cnf"
FcgidInitialEnv MAGICK_HOME "/www/imagemagick"
FcgidInitialEnv PERL "F:/www/perl/perl/bin"
FcgidInitialEnv PERL5LIB "F:/www/perl/perl/site/lib;F:/www/perl/perl/lib"
FcgidInitialEnv FFMPEG "/www/php/encoders_new"
FcgidInitialEnv FFMPEG_DATADIR "/www/php/encoders_new/ffpresets"
#
FcgidInitialEnv PHP_FCGI_CHILDREN 0
#
# Set PHP_FCGI_MAX_REQUESTS to greater than or equal to FcgidMaxRequestsPerProcess
# to prevent php-cgi process from exiting before all requests completed
FcgidInitialEnv PHP_FCGI_MAX_REQUESTS 1000
#
# php.ini defaults to cgi.fix_pathinfo = 1
# instructions say mirror php.ini value, but Joomla won't work
# perhaps this applies the path fix again?
# Set 1, if cgi.fix_pathinfo=1 in php.ini
FcgidFixPathinfo 0
#
# The FastCGI application must begin generating the response within this period of time.
# Number of seconds of idle time before a php-cgi process is terminated (Default: 40)
FcgidIOTimeout 8200
#
# Application processes which have not handled a request for this period of time will be terminated,
# if the number of processses for the class exceeds FcgidMinProcessesPerClass (3) (Default: 300) 240
FcgidIdleTimeout 300
#
# FastCGI application will be killed after handling a request for FcgidBusyTimeout (Default: 300)
FcgidBusyTimeout 300
#
# Scan interval for busy timeout process (Default: 120)
FcgidBusyScanInterval 120
#
# Scan interval for exited process (Default: 3)
FcgidErrorScanInterval 3
#
# Full path to FastCGI access checker (Default: none)
#FcgidAccessChecker
#
# Set to 'off' to allow access control to be passed along to lower modules upon failure (On|Off, Default: On)
#FcgidAccessCheckerAuthoritative On
#
# Full path to FastCGI authenticator (Default: none)
#FcgidAuthenticator
#
# Set to 'off' to allow authentication to be passed along to lower modules upon failure (On|Off, Default: On)
#FcgidAuthenticatorAuthoritative On
#
# Full path to FastCGI authorizer (Default: none)
#FcgidAuthorizer
#
# Set to 'off' to allow authorization to be passed along to lower modules upon failure (On|Off, Default: On)
#FcgidAuthorizerAuthoritative On
#
# Scan interval for idle timeout process (Default: 120)
FcgidIdleScanInterval 120
#
# Maximum size of a request which will be held in memory (Default: 65536)
FcgidMaxRequestInMem 65536
#
# Shared memory file path (Default: logs/fcgid_shm)
#FcgidProcessTableFile "../logs/fcgid_shm"
#
# Directory for AF_UNIX sockets (Unix) or pipes (Windows) (Default: logs/fcgidsock)
#FcgidIPCDir "../logs/fcgidsock"
#
# Each spawn adds this value to the process activity score (Default: 1)
#FcgidSpawnScore 1
#
# Maximum value of the process activity score which allows a spawn to occur (Default: 10)
# Limit on rate of spawning new application processes
#FcgidSpawnScoreUpLimit 10
#
# Each terminated process adds this value to the process activity score (Default: 2)
#FcgidTerminationScore 2
#
# Set processing options for a FastCGI command
#FcgidCmdOptions
#
# Job Control orphan prevention for fcgi workers. (On|Off, Default: Off)
FcgidWin32PreventOrphans Off
#
# Maximum period of time the module will wait while trying to connect to a FastCGI application on Windows (Default: 3).
FcgidConnectTimeout 400
#
# Maximum number of FastCGI application processes which can be active at one time (Default: 1000).(32)
# Global limit on number of processes
FcgidMaxProcesses 1000
#
# Set to 1/2 FcgidMaxProcesses so one VirtualHost can't take up all processes. (Default: 100) 300
# Limit on number of processes per application
FcgidMaxProcessesPerClass 500
#
# Scan interval for zombie process (Default: 3)
FcgidZombieScanInterval 20
#
# Maximum HTTP request length (Default: 131072) in Bytes
FcgidMaxRequestLen 1000000000
#
# Amount subtracted from process activity score each second (Default: 1)
FcgidTimeScore 3
#
# CGI output buffer size (Default: 65536) 64 100
FcgidOutputBufferSize 65536
#
# Maximum FastCGI application process lifetime (Default: 3600)
FcgidProcessLifeTime 8200
#
# Each FastCGI application process will be terminated after handling the specified number of requests.
# A value of 0 disables the check.
FcgidMaxRequestsPerProcess 1000
#
# Min process count of one class of FastCGI application (Default: 3) 0
FcgidMinProcessesPerClass 0
#
# Needed for user authorization (login) to work (with phpmyadmin and all other scripts)
# Header name which will be passed to FastCGI as environment variable
FcgidPassHeader Authorization
FcgidPassHeader Proxy-Authorization
FcgidPassHeader HTTP_AUTHORIZATION
#
#FcgidPassHeader Range
#FcgidPassHeader If-Range
#FcgidPassHeader If-Unmodified-Since
#FcgidPassHeader If-Modified-Since
#FcgidPassHeader If-Match
#FcgidPassHeader If-None-Match
#
# environment variables for the IIP server
#
Include "conf/extra/mod_fcgid.conf"
</IfModule>
在第一个规格中一切正常。构造函数调用的方法返回元素的test('imageParentTest', () => {
document.body.innerHTML = '<div><img class="foo"></div>';
new HtmlElementsCheck()
// assertions for checking parent, returns <div>
})
节点列表,我能够检查该列表,比较图像的父级并进行必要的操作。到目前为止一切顺利。
如果我现在现在分别运行以下规范(document.querySelectorAll('.' + this._configuration.className);
),它也将正确运行,但是,如果我运行共享测试上下文的两个规范(因此如果我运行imageParentTest2
) ,testContext
规范不起作用,因为imageParentTest2
不断返回document.querySelectorAll('.' + this._configuration.className);
插入到正文中的元素列表。例如
imageParentTest
我已经尝试了一种解决方案,该解决方案应该通过添加
重设文档,如this stack overflow answer中所述describe('testContext', () => {
test('imageParentTest', () => {
document.body.innerHTML = '<div><img class="foo"></div>';
new HtmlElementsCheck();
// assertions for checking parent, returns <div>
});
test('imageParentTest2', () => {
document.body.innerHTML = '<picture><img class="foo"></picture>';
new HtmlElementsCheck();
// assertions for checking parent, returns <div> when running test via 'testContext',
// returns <picture> when running test 'imageParentTest2' in isolation
})
})
然而,据我所知,在afterEach(() => {
document.getElementsByTagName('html')[0].innerHTML = '';
});
开头的在我的情况下就没有必要了,因为无论如何我都会在每个测试中设置testContext
。
我还尝试在document.body.innerHTML
中添加jest.resetModules();
以确保不会实例化要实例化的类,并且尝试使用beforeEach
标志运行测试。
那么,在我的第二个测试中,--no-cache
类中的document.querySelectorAll('.' + this._configuration.className);
怎么可能返回在第一个测试中插入到主体中的元素的节点列表?
编辑:我刚刚验证过,该类本身是在单独的测试中的新实例,无论我是单独运行它们还是通过上下文运行它们,因此它看起来越来越像主体在某种情况下不会被覆盖我在上下文中运行了两个测试
答案 0 :(得分:-1)
好吧,看来问题不在于文档本身是否持久,而在于beforeAll
中,它嘲笑了在我的类中执行的函数。在模拟时,我的第二个测试检测到该模拟被第一个测试的参数调用。
因此,该模拟实际上是导致问题的原因(即使第二次测试断言该函数未得到调用)。
更改要在beforeEach
中声明的模拟程序可以解决此问题。